JSTL 1.1 : Paginate Table Example with Next Previous Features

Now a days, paginate table is a common requirement for web application. Although there is no default or standard way to implement pagination in JSP or JSTL, we can use this by using custom tag or JSTL tag.
In this example I want to implement pagination in jsp page using JSTL tag.

First you need to have a MySql datatable, use any one of them. Now create the following two jsp pages.

pagination.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<sql:setDataSource var="dataSource" driver="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost:3306/dbname" user="root" password="dbpass" />

<sql:query dataSource="${dataSource}" var="categories" scope="session">
   		SELECT * FROM your_table_name
</sql:query>

<c:import url="dispresult.jsp?pageNumber=1"/>

dispresult.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<c:set var="categories" value="${sessionScope.categories}" />
<c:set var="rowsPerPage" value="2" />
<c:set var="pageNumber" value="${param.pageNumber}"/>
<c:set var="a">
    <fmt:formatNumber value="${categories.rowCount/rowsPerPage}" maxFractionDigits="0"/>
</c:set>

<c:set var="b" value="${categories.rowCount/rowsPerPage}" />


<c:choose>
    <c:when test="${a==0}">
        <c:set var="numberOfPages" value="1" scope="session"/>   
    </c:when>

    <c:when test="${b>a}">
        <c:set var="xxx" value="${b%a}"/>
        <c:if test="${xxx>0}">
            <c:set var="numberOfPages" value="${b-xxx+1}" scope="session"/>   
        </c:if>
    </c:when>

    <c:when test="${a>=b}">
        <c:set var="numberOfPages" value="${a}" scope="session"/>	   
    </c:when>
</c:choose>

<c:set var="start" value="${pageNumber*rowsPerPage-rowsPerPage}"/>
<c:set var="stop" value="${pageNumber*rowsPerPage-1}"/>
<center>
    <table border="1" width="40%">			    	
        <c:set var="columns" value="0" scope="session"/>
        <c:forEach items="${categories.columnNames}" var="name">
            <c:set var="columns" value="${columns+1}"/>
            <td bgcolor="silver"><c:out value="${name}"/></td>
        </c:forEach>
        <c:set var="columns" value="${columns-1}"/>

        <c:forEach items="${categories.rowsByIndex}" var="row" begin="${start}" end="${stop}">
            <tr>
                <c:forEach begin="0" end="${columns}" var="x">
                    <td><c:out value="${row[x]}"/></td>
                </c:forEach>
            </tr>
        </c:forEach>
    </table>

    <%--For displaying Previous link --%>
    <c:if test="${pageNumber gt 1}">
        <a href="dispresult.jsp?pageNumber=${pageNumber - 1}">Previous</a>
    </c:if>
    <c:forEach begin="1" end="${numberOfPages}" var="i">
        <c:choose>
            <c:when test="${i!=pageNumber}">
                <a href="dispresult.jsp?pageNumber=<c:out value="${i}"/>"><c:out value="${i}"/></a>
            </c:when>
            <c:otherwise>
                <c:out value="${i}"/>
            </c:otherwise>		
        </c:choose>		
    </c:forEach>	
    <%--For displaying Next link --%>
    <c:if test="${pageNumber lt numberOfPages}">
        <a href="dispresult.jsp?pageNumber=${pageNumber + 1}">Next</a>
    </c:if>
</center>

When you run pagination.jsp your output should look like the following with your data table
jstl_pagination

 

admin

Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

34,702` Total Views

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *


one − 1 =