Paging in jsp

Paging in jsp

View Answers

August 22, 2009 at 9:32 AM

Hi Friend,

Try the following code:

pagination.jsp:

<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<%!
public int nullIntconvert(String str){
int num=0;
if(str==null) {
str="0";
}
else if((str.trim()).equals("null")) {
str="0";
}
else if(str.equals("")) {
str="0";
}
try{
num=Integer.parseInt(str);
}
catch(Exception e) { }
return num;
}
%>
<%
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
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) {
iPageNo=0;
}
else{
iPageNo=Math.abs((iPageNo-1)*showRows);
}
String query1="SELECT SQL_CALC_FOUND_ROWS * FROM student limit "+iPageNo+","+showRows+"";
ps1=conn.prepareStatement(query1);
rs1=ps1.executeQuery();

String query2="SELECT FOUND_ROWS() as cnt";
ps2=conn.prepareStatement(query2);
rs2=ps2.executeQuery();
if(rs2.next()) {
totalRows=rs2.getInt("cnt");
}
%>
<html>
<h3>Pagination of JSP page</h3>
<body>
<form>
<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" >
<tr>
<td>Roll No</td>
<td>Name</td>
<td>Marks</td>
<td>Grade</td>
</tr>
<%
while(rs1.next()) {
%>
<tr>
<td><%=rs1.getInt("rollNo")%></td>
<td><%=rs1.getString("name")%></td>
<td><%=rs1.getInt("marks")%></td>
<td><%=rs1.getString("grade")%></td>
</tr>
<%
}
%>
<%
try{
if(totalRows<(iPageNo+showRows)) {
endResult=totalRows;
}
else{
endResult=(iPageNo+showRows);
}
startResult=(iPageNo+1);
totalPages=((int)(Math.ceil((double)totalRows/showRows)));
}
catch(Exception e){
e.printStackTrace();
}
%>
<tr>
<td colspan="3">
<div>

August 22, 2009 at 9:33 AM

continue............

<%
int i=0;
int cPage=0;
if(totalRows!=0) {
cPage=((int)(Math.ceil((double)endResult/(totalRecords*showRows))));

int prePageNo=(cPage*totalRecords)-((totalRecords-1)+totalRecords);
if((cPage*totalRecords)-(totalRecords)>0){
%>
<a href="pagination.jsp?iPageNo=<%=prePageNo%>&cPageNo=<%=prePageNo%>"> << Previous</a>
<%
}
for(i=((cPage*totalRecords)-(totalRecords-1));i<=(cPage*totalRecords);i++){
if(i==((iPageNo/showRows)+1)){%>
<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>
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
<%
try{
if(ps1!=null){
ps1.close();
}
if(rs1!=null){
rs1.close();
}

if(ps2!=null){
ps2.close();
}
if(rs2!=null){
rs2.close();
}

if(conn!=null){
conn.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>

In the above code,we have taken the database table student(rollNo,name,marks,grade).

Thanks









Related Tutorials/Questions & Answers:
JSP Paging issue
JSP Paging issue  Hi; what should I have to write insted of "SQLCALCFOUND_ROWS" for MS-SQL database Amrit
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
Advertisements
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; 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
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 : http://www.roseindia.net/jsp/data-grid.shtml Thanks
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 : http://www.roseindia.net/jsp
JSP Paging Example in Datagrid - JSP-Servlet
JSP Paging Example in Datagrid  Hi, I have tested JSP Paging Example... it successfully. http://www.roseindia.net/jsp/paging.shtml When i try... on the url http://www.roseindia.net/jsp/paging.shtml is customizable or not if yes
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  Hello, This is with refernece to the paging smaple provided on the URL http://www.roseindia.net/jsp/paging.shtml... Folder Name is datagridsample that has a jsp page named search.jsp WEBINF\lib
JSP Paging Example in Datagrid
JSP Paging Example in Datagrid   ... to create paging in JSP. Two files are used "paging.jsp" and "...; ArrayList empList = new ArrayList (); 6). Using  the Taglib to create paging
Paging
Paging  How does paging in struts by use oracle database? Thanks in advance
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... display this record in paging which is having 10 max records in one page
paging - Ajax
paging  i want sample code for paging using pager taglibs
how to make paging with class and ajax
how to make paging with class and ajax  paging with class and ajax
Paging or pagination - Development process
Paging or pagination  1.how to do paging or pagination in jsp using servlets? I have a 20 records ,i have to show them in 2 pages like pages 1 2... to get a correct values... I m sending my jsp code also please help me sir
ModuleNotFoundError: No module named 'django-paging'
ModuleNotFoundError: No module named 'django-paging'  Hi, My... 'django-paging' How to remove the ModuleNotFoundError: No module named 'django-paging' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-paging'
ModuleNotFoundError: No module named 'django-paging'  Hi, My... 'django-paging' How to remove the ModuleNotFoundError: No module named 'django-paging' error? Thanks   Hi, In your python
how to make paging with function ?
how to make paging with function ?  how to make paging with function... "); //this page in only function call and next page in function body paging($offset... Document <?php</p> function paging($offset,$rowsPerPage,$query
ModuleNotFoundError: No module named 'flask-sqlalchemy-paging'
ModuleNotFoundError: No module named 'flask-sqlalchemy-paging'  Hi...: No module named 'flask-sqlalchemy-paging' How to remove the ModuleNotFoundError: No module named 'flask-sqlalchemy-paging' error? Thanks   
ModuleNotFoundError: No module named 'flask-sqlalchemy-paging'
ModuleNotFoundError: No module named 'flask-sqlalchemy-paging'  Hi...: No module named 'flask-sqlalchemy-paging' How to remove the ModuleNotFoundError: No module named 'flask-sqlalchemy-paging' error? Thanks   
paging in php - Design concepts & design patterns
paging in php   comments() Name: Title: Description: i need paging code for this page sir by using only php for every page 10 records will printed. for every
how to make paging with class and ajax
how to make paging with class and ajax  paging with class and ajax...; paging with class and ajax function showUser...) { document.getElementById("paging").innerHTML=xmlhttp.responseText
printing records from jsp - JSP-Servlet
, For paging in Jsp visit to : http://www.roseindia.net/jsp/paging.shtml http://www.roseindia.net/jsp/bank.shtml Thanks
jsp
jsp  how jsp translated into servlets
JSP
JSP  How to run jsp program in eclipse
JSP
what is JSP forward tag for  what is JSP forward tag for   It forwards the current request to another JSP page. Below is the syntax for the same:- <jsp:forward page="...url..." /> We can also forward parameter
jsp
jsp  how to include two jsp page
jsp
jsp  how to create jsp page
Jsp
Jsp  Can I implement interface in jsp
jsp
jsp  write a code for jsp file generator to generate a jsp page automatically taking html page as input
jsp
jsp  how to create a table in oracle using jsp and the table name is entered in text feild of jsp page
JSP
JSP  Hi, What is JSP? What is the use of JSP? Thanks   Hi, JSP Stands for Java Server Pages. It is Java technology for developing web applications. JSP is very easy to learn and it allows the developers to use Java
jsp
jsp   what is the basic necessities reguired for running jsp using JDBC bridge?? and how to write a jsp code to access the detail present in microsoft access pls send link regarding above
jsp
jsp  how to pass jsp variable to an html page
JSP
JSP  relevant difference between servlet and jsp in 1 line
jsp
jsp  how to include google map in our jsp page
JSP
JSP   How to create CAPTCHA creation by using JSP? I need an code for CAPTCHA creation by using JSP...   You can create a captcha validation using jsp. Have a look at the following tutorial: Captcha Creation
jsp
jsp  how to write a servlet and jsp program for user login form
jsp
jsp  how to write a servlet and jsp program for user login form
jsp
jsp  why jsp is translated into servlet?pls describe precisly
jsp
jsp  code for jsp automatic genration using html
jsp
jsp  how to write hindi in jsp and store in database as unicode
JSP
JSP  FILE UPLOAD-DOWNLOAD code USING JSP
jsp
jsp  how to delete data from database using jsp
jsp
jsp  I Want complete note on JSP servlet
jsp
jsp  how to upload image through csv format in jsp
JSP
What are JSP directives, page directives and include directives?  what are jsp directives? and what are page directives? and what are include directives? can you explain taglib directives?   JSP directives JSP
JSP
Can you explain jsp page life cycle what is el how does el search for an attribute in JSP  Can you explain jsp page life cycle what is el how does el...://www.roseindia.net/jsp/simple-jsp-example/LifeCycleOfJspPage.shtml http
JSP
JSP  what are the different scopes an object can have in a jsp page   JSP Scope Variables: Page scope-It makes the data available... the following links: http://www.roseindia.net/jsp/simple-jsp-example

Ads