Ankit Dweivedi
pagination.jsp
1 Answer(s)      5 years and 6 months ago
Posted in : JSP-Servlet
<p>&lt;%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
&lt;%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%></p>

<p><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
&lt;%@ page import="dataBase.*" %>
&lt;%@ page import="java.sql.*" %>
&lt;%!
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;
}
%>
&lt;%
Connection conn = null;
//Here using package for database Connection and it is correct...
conn=DbConnection.getConnection();
ResultSet rs1 = null;
ResultSet rs2 = null;
PreparedStatement ps1=null;
PreparedStatement ps2=null;</p>

<p>int showRows=5;
int totalRecords=5;
int totalRows=nullIntconvert(request.getParameter("totalRows"));
int totalPages=nullIntconvert(request.getParameter("totalPages"));
int iPageNo=nullIntconvert(request.getParameter("iPageNo"));
int cPageNo=nullIntconvert(request.getParameter("cPageNo"));</p>

<p>int startResult=0;
int endResult=0;
if(iPageNo==0){
iPageNo=0;
}
else{
iPageNo=Math.abs((iPageNo-1)*showRows);
}
String query1="SELECT SQL<em>CALC</em>FOUND_ROWS * FROM db2admin.reglimit "+iPageNo+","+showRows+"";
ps1=conn.prepareStatement(query1);
 rs1=ps1.executeQuery();</p>

<p>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="&lt;%=iPageNo%>">
<input type="hidden" name="cPageNo" value="&lt;%=cPageNo%>">
<input type="hidden" name="showRows" value="&lt;%=showRows%>">
<table width="100%" cellpadding="0" cellspacing="0" border="1" >
<tr>
<th>Name</th><th>Address</th><th>Contact</th><th>Email</th>
</tr>
&lt;%
while(rs1.next()){
%>
<tr><td>&lt;%=rs1.getString("USERNAME")%></td>
<td>&lt;%=rs1.getString("FIRSTNAME")%></td>
<td>&lt;%=rs1.getInt("EMAIL")%></td>
<td>&lt;%=rs1.getString("COUNTRY")%></td>
</tr>
&lt;%}%>
&lt;%
try{
if(totalRows&lt;(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>
&lt;%
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=&lt;%=prePageNo%>&amp;cPageNo=&lt;%=prePageNo%>">Previous</a>
&lt;%
}
for(i=((cPage<em>totalRecords)-(totalRecords-1));i&lt;=(cPage</em>totalRecords);i++){
if(i==((iPageNo/showRows)+1)){%>
<a href="pagination.jsp?iPageNo=&lt;%=i%>" style="cursor:pointer;color: red"><b>&lt;%=i%></b></a>
&lt;%
}
else if(i&lt;=totalPages){
%>
<a href="pagination.jsp?iPageNo=&lt;%=i%>">&lt;%=i%></a>
&lt;%
}
}
if(totalPages>totalRecords &amp;&amp; i<totalPages){
%>
<a href="pagination.jsp?iPageNo=&lt;%=i%>&amp;cPageNo=&lt;%=i%>">Next</a>
&lt;%
}
}
%>
<b>Rows &lt;%=startResult%> - &lt;%=endResult%> Total Rows &lt;%=totalRows%> </b>
</div></td></tr></table></form></body></html></p>

Ads

View Answers

April 22, 2011 at 10:47 AM


<%@ 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;
}
%>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connectionconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2admin","root", "root");
 int showRows=5;
int totalRecords=5;
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 db2admin.reg limit "+iPageNo+","+showRows+"";
PreparedStatement ps1=conn.prepareStatement(query1);
ResultSet rs1=ps1.executeQuery();

String query2="SELECT FOUND_ROWS() as cnt";
PreparedStatement ps2=conn.prepareStatement(query2);
ResultSet 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>
<th>USERNAME</th><th>FIRSTNAME</th><th>EMAIL</th><th>COUNTRY</th>
</tr>
<%while(rs1.next()){
%>
<tr><td><%=rs1.getString("USERNAME")%></td>
<td><%=rs1.getString("FIRSTNAME")%></td>
<td><%=rs1.getString("EMAIL")%></td>
<td><%=rs1.getString("COUNTRY")%></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>
<%
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="paging.jsp?iPageNo=<%=prePageNo%>&cPageNo=<%=prePageNo%>">Previous</a>
<%
}
for(i=((cPage*totalRecords)-(totalRecords-1));i<=(cPage*totalRecords);i++){
if(i==((iPageNo/showRows)+1)){%>
<a href="paging.jsp?iPageNo=<%=i%>" style="cursor:pointer;color: red"><b><%=i%></b></a>
<%
}
else if(i<=totalPages){
%>
<a href="paging.jsp?iPageNo=<%=i%>"><%=i%></a>
<%
}
}
if(totalPages>totalRecords && i<totalPages){
%>
<a href="paging.jsp?iPageNo=<%=i%>&cPageNo=<%=i%>">Next</a>
<%
}
}
%>
<b>Rows <%=startResult%> - <%=endResult%> Total Rows <%=totalRows%> </b>
</div></td></tr></table></form></body></html>









Related Tutorials/Questions & Answers:
pagination.jsp
pagination.jsp  org.apache.jasper.JasperException: An exception occurred processing JSP page /jsp/pagination.jsp at line 54 51: } 52: String query1="SELECT SQLCALCFOUND_ROWS * FROM db2admin.reglimit "+iPageNo+","+showRows+""; 53
pagination.jsp:
pagination.jsp:  After runing this jsp it again gives exception . pls can u tell what is reason behind this ?? An Exception.../pagination.jsp at line 49 46: } 47: String query1="SELECT SQLCALCFOUND_ROWS * FROM
Advertisements
pagination.jsp
pagination.jsp  <p>&lt;%@ page language="java" import...; <a href="pagination.jsp?iPageNo=&lt;%=prePageNo%>&amp;cPageNo=&...++){ if(i==((iPageNo/showRows)+1)){%> <a href="pagination.jsp?iPageNo=&lt
pagination.jsp
pagination.jsp  <p>&lt;%@ page language="java" import...; <a href="pagination.jsp?iPageNo=&lt;%=prePageNo%>&amp;cPageNo=&...++){ if(i==((iPageNo/showRows)+1)){%> <a href="pagination.jsp?iPageNo=&lt
JSP
the following code: pagination.jsp: <%@ page language="java" %> <%@ page import...)-(totalRecords)>0){ %> <a href="pagination.jsp?iPageNo=<%=prePageNo...==((iPageNo/showRows)+1)){%> <a href="pagination.jsp?iPageNo=<%=i%>
display 10 records per page in jsp - JSP-Servlet
: pagination.jsp: Pagination of JSP page Roll No Name Marks
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  Hi Friend, Try the following code: pagination.jsp
JSP - JSP-Interview Questions
: 'pagination.jsp' Pagination of JSP page Roll No Name Marks
pagination
. pagination.jsp: <%@ page language="java" %> <%@ page import="java.sql....*totalRecords)-(totalRecords)>0){ %> <a href="pagination.jsp?iPageNo=<...="pagination.jsp?iPageNo=<%=i%>" style="cursor:pointer;color: red"><
how can manages recordset in jsp
  pagination.jsp: <%@ page import="java.sql.*" %> <%! public...*totalRecords)-(totalRecords)>0){ %> <a href="pagination.jsp?iPageNo=<...++){ if(i==((iPageNo/showRows)+1)){%> <a href="pagination.jsp?iPageNo=<%=i
auto resize
using jsp. Here is the code: pagination.jsp: <%@ page language="java" %>...); if((cPage*totalRecords)-(totalRecords)>0){ %> <a href="pagination.jsp...*totalRecords);i++){ if(i==((iPageNo/showRows)+1)){%> <a href="pagination.jsp
pagination in hibernate with jsp
. pagination.jsp: <%@ page language="java" %> <%@ page import="java.sql.*" %> <...*totalRecords)-(totalRecords)>0){ %> <a href="pagination.jsp?iPageNo=<...++){ if(i==((iPageNo/showRows)+1)){%> <a href="pagination.jsp?iPageNo=<%=i
jsf pagination
;0){ %> <a href="pagination.jsp?iPageNo=<%=prePageNo%>&cPageNo.../showRows)+1)){%> <a href="pagination.jsp?iPageNo=<%=i%>" style="cursor... if(i<=totalPages){ %> <a href="pagination.jsp?iPageNo=<%=i%>"><
pagination
)-(totalRecords)>0){ %> <a href="pagination.jsp?iPageNo=<%=prePageNo%>...==((iPageNo/showRows)+1)){%> <a href="pagination.jsp?iPageNo=<%=i%>...; <% } else if(i<=totalPages){ %> <a href="pagination.jsp?iPageNo=<
Pagination in jsp using oracle and not sql
){ %> <a href="pagination.jsp?iPageNo=<%=prePageNo%>&cPageNo=<...)){%> <a href="pagination.jsp?iPageNo=<%=i%>" style="cursor:pointer...;=totalPages){ %> <a href="pagination.jsp?iPageNo=<%=i%>"><%=i%>
Pagination
;0){ %> <a href="pagination.jsp?iPageNo=<%=prePageNo%>&cPageNo.../showRows)+1)){%> <a href="pagination.jsp?iPageNo=<%=i%>" style="cursor... if(i<=totalPages){ %> <a href="pagination.jsp?iPageNo=<%=i%>"><
pagination
)-(totalRecords)>0){ %> <a href="pagination.jsp?iPageNo=<%=prePageNo...++){ if(i==((iPageNo/showRows)+1)){%> <a href="pagination.jsp?iPageNo=<%=i%>...; <% } else if(i<=totalPages){ %> <a href="pagination.jsp?iPageNo=<
sort a record in jsp - JSP-Servlet
Answers Hi Friend, Try the following code: pagination.jsp
large nuber of records display to jsp - Development process
specified 10 records per page.Try the following code: pagination.jsp
Pagination Example
="pagination"> <result>/pages/chatQuestion/pagination.jsp</result>... in the pagination.jsp: <%@ taglib prefix="s" uri="/struts-tags" %>
Rich Editor Example
;/pages/chatQuestion/pagination.jsp</result> </action>... pagination code: Here is the code to be added in the pagination.jsp: <
Javascript Fixed Header in A table - Design concepts & design patterns
-Total Rows The above jsp page is named as 'pagination.jsp
Paging or pagination - Development process
..... //Pagination.jsp <% int intEmpId = 0
JSP Pagination
I could possibly use??   JSP Pagination pagination.jsp: <%@ page
What's wrong with my pagination code in JSP?
the given file 'pagination.jsp'. <%@ page import="java.sql.*" %> <%! public

Ads

Advertisement