Pagination Example
When we have large numbers of records which can note be displayed on a single page. Then we use the concept of Pagination. It helps use to display limited number of records in a single page. Below is an example of pagination.
For creating this example below is the directory structure of the program:
pStud.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="PageViewServlet?page=1">View Student</a>
</body>
</html>
PageViewServlet.java
package com.app.studytonight;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/PageViewServlet")
public class PageViewServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request1, HttpServletResponse response1) throws ServletException, IOException
{
response1.setContentType("text/html");
PrintWriter out=response1.getWriter();
String spageid1=request1.getParameter("page");
int pageid1=Integer.parseInt(spageid1);
int total1=5;
if(pageid1==1){}
else{
pageid1=pageid1-1;
pageid1=pageid1*total1+1;
}
List<pStudGetSet> list=StudDao.getRecords(pageid1,total1);
out.print("<h1>studytonight Page No: "+spageid1+"</h1>");
out.print("<table border='1' cellpadding='4' width='60%'>");
out.print("<tr><th>Id</th><th>Name</th><th>Age</th><th>City</th></tr>");
for(pStudGetSet e1:list){
out.print("<tr><td>"+e1.getId1()+"</td><td>"+e1.getName1()+"</td><td>"+e1.getAge1()+"</td></td>"+e1.getCity1()+"</td></tr>");
}
out.print("</table>");
out.print("<a href='PageViewServlet?page=1'>1</a> ");
out.print("<a href='PageViewServlet?page=2'>2</a> ");
out.print("<a href='PageViewServlet?page=3'>3</a> ");
out.close();
}
}
pStudGetSet.java
package com.app.studytonight;
public class pStudGetSet {
private int id1;
private String name1, age1, city1;
public int getId1() {
return id1;
}
public void setId1(int id1) {
this.id1 = id1;
}
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
public String getAge1() {
return age1;
}
public void setAge1(String age1) {
this.age1 = age1;
}
public String getCity1() {
return city1;
}
public void setCity1(String city1) {
this.city1 = city1;
}
StudDao.java
package com.app.studytonight;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class StudDao {
public static Connection getConnection(){
Connection con1=null;
try{
Class.forName("com.mysql.jdbc.Driver");
con1=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","System","oracle");
}
catch(Exception e)
{
System.out.println(e);
}
return con1;
}
public static List<pStudGetSet> getRecords(int start1,int total1){
List<pStudGetSet> list=new ArrayList<pStudGetSet>();
try{
Connection con1=getConnection();
PreparedStatement ps1=con1.prepareStatement("select * from student limit "+(start1-1)+","+total1);
ResultSet rs1=ps1.executeQuery();
while(rs1.next()){
pStudGetSet e1=new pStudGetSet();
e1.setId1(rs1.getInt(1));
e1.setName1(rs1.getString(2));
e1.setAge1(rs1.getString(3));
e1.setCity1(rs1.getString(4));
list.add(e1);
}
con1.close();
}catch(Exception e){System.out.println(e);}
return list;
}
}