To divide large number of records into multiple parts, we use pagination. It allows user to display a part of records only. Loading all records in a single page may take time, so it is always recommended to created pagination. In servlet, we can develop pagination example easily.

In this servlet pagination example, we are using MySQL database to fetch records.

Here, we have created “emp” table in “test” database. The emp table has three fields: id, name and salary. Either create table and insert records manually or import our sql file.

index.html

ViewServlet.java

package com.javatpoint.servlets;  
  
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;  
import com.javatpoint.beans.Emp;  
import com.javatpoint.dao.EmpDao;  
  
@WebServlet("/ViewServlet")  
public class ViewServlet extends HttpServlet {  
    private static final long serialVersionUID = 1L;  
    protected void doGet(HttpServletRequest request, HttpServletResponse response)   
           throws ServletException, IOException {  
        response.setContentType("text/html");  
        PrintWriter out=response.getWriter();  
          
        String spageid=request.getParameter("page");  
        int pageid=Integer.parseInt(spageid);  
        int total=5;  
        if(pageid==1){}  
        else{  
            pageidpageid=pageid-1;  
            pageidpageid=pageid*total+1;  
        }  
        List list=EmpDao.getRecords(pageid,total);  
  
        out.print("Page No: "+spageid+"");  
        out.print("");  
        out.print("");  
        for(Emp e:list){  
            out.print("");  
        }  
        out.print("");  
        out.print("1 ");  
        out.print("2 ");  
        out.print("3 ");  
        out.close();  
    }  
}

Emp.java

package com.javatpoint.beans;  
  
public class Emp {  
private int id;  
private String name;  
private float salary;  
//getters and setters  
}

EmpDao.java

package com.javatpoint.dao;  
import com.javatpoint.beans.*;  
import java.sql.*;  
import java.util.ArrayList;  
import java.util.List;  
public class EmpDao {  
  
    public static Connection getConnection(){  
        Connection con=null;  
        try{  
            Class.forName("com.mysql.jdbc.Driver");  
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","","");  
        }catch(Exception e){System.out.println(e);}  
        return con;  
    }  
  
    public static List getRecords(int start,int total){  
        List list=new ArrayList();  
        try{  
            Connection con=getConnection();  
            PreparedStatement ps=con.prepareStatement("select * from emp limit "+(start-1)+","+total);  
            ResultSet rs=ps.executeQuery();  
            while(rs.next()){  
                Emp e=new Emp();  
                e.setId(rs.getInt(1));  
                e.setName(rs.getString(2));  
                e.setSalary(rs.getFloat(3));  
                list.add(e);  
            }  
            con.close();  
        }catch(Exception e){System.out.println(e);}  
        return list;  
    }  
}

Output

Servlet Pagination Example 1 Servlet Pagination Example 2 Servlet Pagination Example 3 Servlet Pagination Example 4