Need to implement Paging and field based sorting in JSP Servlet
2 Answer(s)      7 years ago
Posted in : JSP-Servlet


View Answers

June 27, 2009 at 4:51 PM

Hi Friend

For pagination,try the following code:

<%@ page language="java" %>
<%@ page import="java.sql.*" %>
public int nullIntconvert(String str){
int num=0;
if(str==null) {
else if((str.trim()).equals("null")) {
else if(str.equals("")) {
catch(Exception e) { }
return num;
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root";, "root");
ResultSet rs1 = null;
ResultSet rs2 = null;
PreparedStatement ps1=null;
PreparedStatement ps2=null;

int showRows=10;
int totalRecords=10;
int totalRows=nullIntconvert(request.getParameter("totalRows"));
int totalPages=nullIntconvert(request.getParameter("totalPages"));
int iPageNo=nullIntconvert(request.getParameter("iPageNo"));
int cPageNo=nullIntconvert(request.getParameter("cPageNo"));

int startResult=0;
int endResult=0;
if(iPageNo==0) {
String query1="SELECT SQL_CALC_FOUND_ROWS * FROM student limit "+iPageNo+","+showRows+"";

String query2="SELECT FOUND_ROWS() as cnt";
if( {
<h3>Pagination of JSP page</h3>
<input type="hidden" name="iPageNo" value="<%=iPageNo%>">
<input type="hidden" name="cPageNo" value="<%=cPageNo%>">
<input type="hidden" name="showRows" value="<%=showRows%>">
<table width="100%" cellpadding="0" cellspacing="0" border="1" >
<td>Roll No</td>
while( {
if(totalRows<(iPageNo+showRows)) {
catch(Exception e){
<td colspan="3">

June 27, 2009 at 4:51 PM


int i=0;
int cPage=0;
if(totalRows!=0) {

int prePageNo=(cPage*totalRecords)-((totalRecords-1)+totalRecords);
<a href="pagination.jsp?iPageNo=<%=prePageNo%>&cPageNo=<%=prePageNo%>"> << Previous</a>
<a href="pagination.jsp?iPageNo=<%=i%>" style="cursor:pointer;color: red"><b><%=i%></b></a>
else if(i<=totalPages){
<a href="pagination.jsp?iPageNo=<%=i%>"><%=i%></a>
if(totalPages>totalRecords && i<totalPages){
<a href="pagination.jsp?iPageNo=<%=i%>&cPageNo=<%=i%>"> >> Next</a>
<b>Rows <%=startResult%> - <%=endResult%> Total Rows <%=totalRows%> </b>


catch(Exception e)


Related Tutorials/Questions & Answers:
Need to implement Paging and field based sorting in JSP Servlet - JSP-Servlet
Need to implement Paging and field based sorting in JSP Servlet  Hi, Can some one please guide me how we can implement the paging and field based sorting on records. Following is my requirement: Suppose we fetch the record
Paging in JSP - JSP-Servlet
Paging in JSP  Sir, The error shows in SELECT Query while executing the following query in Paging "SELECT * FROM tablename LIMIT 0,10" What is the exact query in MsAccess for Using Limit.Thsmk you for ur replies
paging in Jsp: jsp code - JSP-Servlet
paging in Jsp: jsp code  Sir/Madam- i m working on jsp . i want to perform paging in jsp file so that we don't need to scroll page... friend, pagination using jsp with database <
JSP Paging Example in Datagrid - JSP-Servlet
JSP Paging Example in Datagrid  Hi, I have tested JSP Paging Example... it successfully. When i try... on the url is customizable or not if yes
JSP Paging Example in Datagrid - JSP-Servlet
JSP Paging Example in Datagrid  Hello, This is with refernece to the paging smaple provided on the URL Folder Name is datagridsample that has a jsp page named search.jsp WEBINF\lib
Sorting a jsp page. - JSP-Servlet
Sorting a jsp page.  Hi there, It's me again. I tried to use Ajax... is process the code in the servlet. Here's the code in the jsp below: JSP Page Universal Books Home Page
Server side Paging in jsp ?
Server side Paging in jsp ?  Hi i am student of It . i am learning jsp-servlet and i am trying to do paging in my report generation but i want to do server side paging and i am using oracle 10g in back end . it will be very
JSP Paging issue
JSP Paging issue  Hi; what should I have to write insted of "SQLCALCFOUND_ROWS" for MS-SQL database Amrit
rich text field - JSP-Servlet
rich text field  Hi, this is jawahar. my question i need rich text field in my program . plz send me the code
JSP Paging issue
JSP Paging issue  Hi; How to display large number of users- account profile with photo can be placed in continuous pages using JSP code . If any one have solution please help me . Amrit;   Visit Here
Sorting data in a jsp using a servlet while implementing comparable - JSP-Servlet
Sorting data in a jsp using a servlet while implementing comparable  ... at line 14 of the jsp code. I also want it to implement comparable to use... for displaying them in the jsp: 1. Universal Books Home Page 2. 3. 4
How to implement session variables - JSP-Servlet
print it. Do I need to add any path like (@servlet) because how the jsp know...How to implement session variables  Hi, I have a servlet which gets parameter from a jsp page(x). it is stored in a variable in servlet as shown
Paging in jsp - Java Beginners
Paging in jsp  Hi all, I want to make paging in jsp please send the code and process also. Like 1,2,3,4. its urgent...please send ...: Pagination of JSP page Roll No Name Marks Grade
datagid with paging using jsp - Ajax
datagid with paging using jsp  datagrid with paging using ajax and jsp  Hi friend, For read more information : Thanks
need a jsp example - JSP-Servlet
need a jsp example  I need a jsp example to send a voice message or a audio .wav file from my pc to a mobile phone
jsp paging - JSP-Interview Questions
jsp paging   I am working in JSP paging with ms-access but i have..." is created Correct or not. For more on Paging in JSP visit to : http... or not. For more on Paging in JSP visit to :
Fetching database field from servlet to jsp page ?
Fetching database field from servlet to jsp page ?  Hello Java... field. I wanted to pass some of the database field from servlet to jsp... (i...) field to jsp page . { where 'rs' is Resultset object} please help
Paging  How does paging in struts by use oracle database? Thanks in advance
Need E-Books - JSP-Servlet
Need E-Books  Please i need a E-Book tutorial on PHP please kindly send this to me thanks
I need jsp code for how to write text field value into property file.
I need jsp code for how to write text field value into property file.  Hi , I need to set the text field value into property file using jsp code. Example : Username : Valar , I have entered the value "Valar" in the text field
JSP Paging Example in Datagrid
JSP Paging Example in Datagrid   ... to create paging in JSP. Two files are used "paging.jsp" and "... (); 6). Using  the Taglib to create paging and show records. Step:1
JSP:NEED RESPONSE ASAP PLEASE!  Hi freind, If you could respond... and then sends this data in url to the next jsp page. The code works fine...://localhost:8080/examples/jsp/popup.jsp?name='+content1+'&&dd='+content2,'mywindow
Need to get the Data based on Date
Need to get the Data based on Date  Hie , I am having a criteria where i need to get the data based on date . I have written the query as "from Balance as b where like ?" But the date field in DB is in datetime2 format
How to implement openId java web based application?
How to implement openId java web based application?  In my web application i want to implement the OpenId just like have to login.... Please tell me how to implement it in java web application in details
need to ans abt db - JSP-Servlet
need to ans abt db  helllo can you tell me m just starting to knowing abt servlet and i trying to making e-mail site so can you tell me with the java servlet wht is the backhand database to use to proper with that site
Implement an interface in a JSP
Implement an interface in a JSP  Can we implement an interface in a JSP?   
javascript cal code for selecting date in text field - JSP-Servlet
javascript cal code for selecting date in text field  HI I want javascript calendar code to select date in text field in jsp.pls send me? .../jsp/emp-event.shtml
urgent need in two days - JSP-Servlet
the Given JSP Example Code-------------------------------Servletform.html<...; version="2.4"><servlet> <servlet-name>InsertDataExample</servlet-name> <servlet-class>DataInsertServletExam</servlet
Need help in image uploading - JSP-Servlet
Need help in image uploading  hii, i have one doubt in uploading an image in jsp. I want to display image from DB to my jsp page. For that i... a folder named images in server but cant upload.Is any extra code need for uploading
Need alternative for getParameter() method - JSP-Servlet
Need alternative for getParameter() method  Hi Expert, I would like to know if if any other way to fetch the vaule from text box withour using getParameter() method. Kindly requesting you to let me know it, If any way
Need Help in creating online quiz application using JSP - JSP-Servlet
Need Help in creating online quiz application using JSP  Hi, i am creating online Quiz application using JSP and MySQl ,Apache 6 in Netbeans IDE. i... the answer is correct or not,,but i can't implement the program further ,in such way