Pagination with condition using servlet or jsp

Ads

 
 

Share on Google+Share on Google+

veerabhadrarao.balla
Pagination with condition using servlet or jsp
1 Answer(s)      4 years and 9 months ago
Posted in : JSP-Servlet

Hi, Thanks in advance. I have a requirement like this,

In front-end page I have one text box and one submit button, After clicking the submit button that form field value I retrieved using "String name=request.getParameter("name of that text box")", Then I want perform the pagination based on the string value(I mean pagination with condition). My problem is first time five records will come(I mean 5 records per page) after clicking the 2 page that page will be reload that time "name variable will become null" finally result will come null only (Totally matched records i have 25 based on the condition) and I found one problem in that when I am trying to click 2 page onwords that time name variable will become null. How to make it that name variable constant.

Ads
View Answers

December 10, 2012 at 3:08 PM


Here is jsp code that retrieves the record from database and show pagination.

paging.jsp:

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

Ads









Related Tutorials/Questions & Answers:
Pagination with condition using servlet or jsp
Pagination with condition using servlet or jsp  Hi, Thanks... I retrieved using "String name=request.getParameter("name of that text box")", Then I want perform the pagination based on the string value(I mean pagination
Pagination in jsp - JSP-Servlet
Pagination in jsp  I need an example of pagination in JSP using display tag
Advertisements
Jsp pagination - JSP-Servlet
has to go to the specified servlet and it has to get the next question can u plz tell me the solution  Hi friend, For Jsp pagination application...Jsp pagination  Iam doing a online exam application for this i need
pagination - JSP-Servlet
friend, Simple pagination in jsp Simple pagination in JSP... Simple pagination in jsp Displaying Records...pagination  Hi, could u tell me how i will handle pagination
how to disable submit button in jsp using servlet when some condition is satisfied
how to disable submit button in jsp using servlet when some condition is satisfied  how to disable submit button in jsp using servlet when some condition is satisfied
Pagination - JSP-Servlet
Pagination  I want to display only 10 records but my arraylist contains for example about 100 records... My jsp page should contain 10 records along with the pagination below.... like prev 1 2 3 4 5 next I am using
pagination problem - JSP-Servlet
(),insert() methods using or importing employee class.finally i created a class using servlets in that i called a these methods,to call this servlet i created...pagination problem  Thanks sir for posting the answer.But i have 2
pagination problem - JSP-Servlet
(),insert() methods using or importing employee class.finally i created a class using servlets in that i called a these methods,to call this servlet i created...pagination problem   Thanks sir for posting the answer.But i have 2
dynamic pagination in jsp - JSP-Servlet
dynamic pagination in jsp  i am unable to display the data in multiple pages. I am gettting data from the database from the javaBeans to jsp by vectors... but i am unable to displaying them in jsp 15 records per page
Pagination in jsp using oracle and not sql
Pagination in jsp using oracle and not sql  I need the code for pagination in jsp using oracle. I already tried it with rownum between instead... Pagination using Mysql database <%@ page language="java" %> <%@ page
In pagination how to hilight the links - JSP-Servlet
In pagination how to hilight the links  Dear Sir, Thanks for posting the answer,its working fine.Now i want google or others like pagination ,in that if i click on a number only that number color will be changed like
In pagination how to hilight the links - JSP-Servlet
In pagination how to hilight the links  Dear Sir, In pagination how to highlight the links or how to change the color of that link when i clickon that particular link....Please help me sir.. Some of the code which i
jsp pagination
jsp pagination  I want to implement pagination on jsp page Each jsp page having some radio buttons, on click of next previous page selected radio buttons are reset. I want to maintain state of selected radio buttons on previous
JSP Pagination
I could possibly use??   JSP Pagination pagination.jsp: <%@ page..."); } %> <html><h3>Pagination of JSP page</h3> <body><...JSP Pagination  Hi , I have several JSP's that displays data from
Pagination Using Displaytag Lib
Pagination Using Displaytag Lib  I am using Struts 1.2 for my web based project. While using display tag lib in jsp for pagination, is there any substitute of using As I dont want to be dependent on any site(s). Please
Pagination
Pagination  How to create pagination in jsp with EJB using MS SQL
pagination in jsp - JDBC
. Simple pagination in jsp... ------------------   Simple pagination in jsp Simple pagination in JSP Displaying Records
Using Scriplet in JSP - JSP-Servlet
Using Scriplet in JSP  Hi, I need to display suitable fields in my jsp but this should be done upon suitable condition becomes true for eg... on } IN HOME JSP ****** home.jsp **** Currently I am directly using
Pagination without using database in php
Pagination without using database in php  How can I show multiple images in multiple rows with pagination in php
How to upload files to server using JSP/Servlet?
How to upload files to server using JSP/Servlet?  How to upload files to server using JSP/Servlet
pagination
pagination   Simple way for pagination in jsp using java script to display data without send database
Jsp using mvc - JSP-Servlet
Jsp using mvc  hi I have written using mvc2 architecture, i have written logic in java ,i want to print a message on the form stating the the values are inserted successfully.So can you tell me how i have to write the code
jsp page authentication panel using jsp/servlet?
jsp page authentication panel using jsp/servlet?  I have 10 jsp jsp forms and 7 users and i want to grant variour permission like edit,delete and save for users dynamically on forms.So please refer me code
Radio button validation using jsp - JSP-Servlet
Radio button validation using jsp  I had one jsp Page and servlet. I did my validations in servlet for my jsp page which contains the radio buttons.Here,the servlet acts as controller and if there is any null(i.e., not choosen
Hibernate JSP
In this section, you will learn about Hibernate with JSP using simple pagination example
online test software using jsp and servlet
online test software using jsp and servlet  online test software using jsp and servlet
pagination in hibernate with jsp
pagination in hibernate with jsp  Hi, plz give me example on pagination .   Hi Friend, Visit Here Thanks
Jsp table Pagination
Jsp table Pagination  I tried the code already.But I dont want it with SQL.I want it for oracle database because I'm using oracle.So please help me with this issue. Thanks
pagination in hibernate with jsp
pagination in hibernate with jsp  Hi, plz give me example on pagination .   Hi Friend, We have used MySql database with JSP...>Pagination of JSP page</h3> <body> <form> <input type
Using a user defined non-packaged servlet class in jsp. - JSP-Servlet
Using a user defined non-packaged servlet class in jsp.  i want know that how to use a user defined java servlet function's, variables and methods of a user defined java servlet in jsp.if possible plz try to explain with a simple
pagination in JSP - JSP-Interview Questions
pagination in JSP  Hi! Everybody.. I am new to JSP. I am doing project using JSP. Everything is OK. When i am displaying the data from ResultSet... --------------------- Visit for more information. http://www.roseindia.net/jsp/ Thanks