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.


<%@ 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

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


<%@ 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 var="b" value="${categories.rowCount/rowsPerPage}" />

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

    <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:when test="${a>=b}">
        <c:set var="numberOfPages" value="${a}" scope="session"/>	   

<c:set var="start" value="${pageNumber*rowsPerPage-rowsPerPage}"/>
<c:set var="stop" value="${pageNumber*rowsPerPage-1}"/>
    <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:set var="columns" value="${columns-1}"/>

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

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

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



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

36,994` Total Views

One comment

Leave a Reply

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

five × = 30