Home » Java » How To Do Pagination In JSP..? [duplicate]

How To Do Pagination In JSP..? [duplicate]

Posted by: admin December 28, 2021 Leave a comment

Questions:

Please do not down rate my question, because i’m new for JSP/JavaEE..

I looked so many tutorials about JSTL, Pagination, but i cannot get a proper idea to do Pagination.. Is there an esay way to perform Pagination..?

In my program, I retrieve records of search results from database using a java class and put them into a ArrayList and returns ArrayList to Servlet. Then assigns the received ArrayList object into an application attribute like this

request.setAttribute("Results", results_List);

then using request dispatcher, i’m loading my result showing jsp page like this.

RequestDispatcher rd = request.getRequestDispatcher("searchresults.jsp");
rd.forward(request, response);

so tell me the next step to do pagination my search reslts..

Answers:

In Servlet, we are first getting the value of ‘page’ parameter and storing it in ‘page’ variable. We wanted to display five (5) records per page which we are passing as an argument to viewAllEmployees(offset, 5). We are storing three attributes in the request scope and forwarding the request to a JSP page;

Servlet Class:

public void doGet(HttpServletRequest request,  HttpServletResponse response)  throws ServletException, IOException {
            int page = 1;
            int recordsPerPage = 5;
            if(request.getParameter("page") != null)
                page = Integer.parseInt(request.getParameter("page"));
            EmployeeDAO dao = new EmployeeDAO();
            List<Employee> list = dao.viewAllEmployees((page-1)*recordsPerPage,
                                     recordsPerPage);
            int noOfRecords = dao.getNoOfRecords();
            int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);
            request.setAttribute("employeeList", list);
            request.setAttribute("noOfPages", noOfPages);
            request.setAttribute("currentPage", page);
            RequestDispatcher view = request.getRequestDispatcher("employee.jsp");
            view.forward(request, response);
        }

Jsp File :

 

<table border="1" cellpadding="5" cellspacing="5">
        <tr>
            <th>Emp ID</th>
            <th>Emp Name</th>
            <th>Salary</th>
            <th>Dept Name</th>
        </tr>
 
        <c:forEach var="employee" items="${employeeList}">
            <tr>
                <td>${employee.employeeId}</td>
                <td>${employee.employeeName}</td>
                <td>${employee.salary}</td>
                <td>${employee.deptName}</td>
            </tr>
        </c:forEach>
    </table>
 
    <%--For displaying Previous link except for the 1st page --%>
    <c:if test="${currentPage != 1}">
        <td><a href="employee.do?page=${currentPage - 1}">Previous</a></td>
    </c:if>
 
    <%--For displaying Page numbers. 
    The when condition does not display a link for the current page--%>
    <table border="1" cellpadding="5" cellspacing="5">
        <tr>
            <c:forEach begin="1" end="${noOfPages}" var="i">
                <c:choose>
                    <c:when test="${currentPage eq i}">
                        <td>${i}</td>
                    </c:when>
                    <c:otherwise>
                        <td><a href="employee.do?page=${i}">${i}</a></td>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
        </tr>
    </table>
     
    <%--For displaying Next link --%>
    <c:if test="${currentPage lt noOfPages}">
        <td><a href="employee.do?page=${currentPage + 1}">Next</a></td>
    </c:if>