Hello Dear Friends
Here I have put Code For Pagination in servlet and jsp. and attached war file. with source code
Resultpage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
ArrayList arrayList = new ArrayList();
String pages;
arrayList =(ArrayList) session.getAttribute("data");
pages = (String)session.getAttribute("pages");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.ArrayList"%>
<%@page import="com.test.mytest.StudentBean"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
int i=0;
while(i<arrayList.size()){
StudentBean student = (StudentBean) arrayList.get(i);
out.println(student.getName_Strudet());
i++;
}
out.println("<br><br>");
int j = 1;
while(j<=Integer.parseInt(pages)+1){
out.println("<a href=PaginationExample?id="+j+">"+j+"</a>");
j++;
}
%>
</body>
</html>
PaginationExample.java (Servlet)
package com.test.mytest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.sun.org.apache.bcel.internal.generic.AALOAD;
/**
* Servlet implementation class PaginationExample
*/
public class PaginationExample extends HttpServlet {
public void service(HttpServletRequest request,HttpServletResponse response) throws IOException{
String pageNo = request.getParameter("id");
if(pageNo==null){
pageNo="1";
}
final int offSet = 5;
int pageNumber = Integer.parseInt(pageNo);
System.out.println(pageNumber);
if(pageNumber != 0){
ArrayList arrayList = new ArrayList();
arrayList.add(new StudentBean("nayan","padra","vadodara"));
arrayList.add(new StudentBean("nayan1","padra","vadodara"));
arrayList.add(new StudentBean("nayan2","padra","vadodara"));
arrayList.add(new StudentBean("nayan3","padra","vadodara"));
arrayList.add(new StudentBean("nayan4","padra","vadodara"));
arrayList.add(new StudentBean("nayan5","padra","vadodara"));
arrayList.add(new StudentBean("nayan6","padra","vadodara"));
arrayList.add(new StudentBean("nayan7","padra","vadodara"));
arrayList.add(new StudentBean("nayan8","padra","vadodara"));
arrayList.add(new StudentBean("nayan9","padra","vadodara"));
arrayList.add(new StudentBean("nayan10","padra","vadodara"));
arrayList.add(new StudentBean("nayan11","padra","vadodara"));
arrayList.add(new StudentBean("nayan12","padra","vadodara"));
HttpSession session = request.getSession();
int numberOfData = arrayList.size();
System.out.println(numberOfData);
int totalPages = numberOfData/offSet;
String noPages = Integer.toString(totalPages);
System.out.println(noPages + 1);
System.out.println("Page No 1");
int nodata = offSet*pageNumber;
int fistNo = nodata - offSet;
System.out.println("NO Of Data"+nodata);
System.out.println("From"+fistNo+"To"+nodata);
if(nodata>arrayList.size()){
nodata = arrayList.size();
}
ArrayList onlyData = new ArrayList();
while (fistNo<nodata ){
StudentBean student = (StudentBean) arrayList.get(fistNo);
System.out.println(student.getName_Strudet());
onlyData.add(student);
fistNo ++;
}
System.out.println(onlyData.size());
session.setAttribute("data",onlyData );
session.setAttribute("pages", noPages);
RequestDispatcher rd = request.getRequestDispatcher("resultPage.jsp");
try {
rd.forward(request, response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
StudentBean.java (Java Bean)
package com.test.mytest;
public class StudentBean {
private String name_Strudet;
private String Address;
private String City;
public StudentBean(String string, String string2, String string3) {
this.name_Strudet=string;
this.Address = string2;
this.City = string3;
}
public String getName_Strudet() {
return name_Strudet;
}
public void setName_Strudet(String nameStrudet) {
name_Strudet = nameStrudet;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
}
Deployment Descriptor file (web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>My Practice</display-name>
<servlet>
<description>
</description>
<display-name>MyTest1</display-name>
<servlet-name>MyTest1</servlet-name>
<servlet-class>
com.test.mytest.MyTest1</servlet-class>
</servlet>
<servlet>
<description>
</description>
<display-name>PaginationExample</display-name>
<servlet-name>PaginationExample</servlet-name>
<servlet-class>
com.test.mytest.PaginationExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyTest1</servlet-name>
<url-pattern>/MyTest1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PaginationExample</servlet-name>
<url-pattern>/PaginationExample</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
i tryed but it not work properly
how u use this Leave reply concept in ur application i want to use in my application give mi idea abotu that plz……………….
hello
“Pagination Using Servlet And Jsp Nayan Rami” definitely causes me ponder a somewhat
extra. I really adored each and every single piece of this post.
Regards ,Naomi