Pagination with condition using servlet or jsp
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.
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
Advertisements
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 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
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
Crop image Using JQuery, servlet or JSP.
Crop image
Using JQuery,
servlet or
JSP. Hi sir
I need the code of Cropping image
using JQuery with the help of
servlet or
JSP. I need both the code Client side as well as of server side.
Please Help me
code for password strength using jsp-servlet
code for password strength
using jsp-servlet hi..............
plz help me to give code for password strength
using jsp-
servlet
for implementation in my project as soon as possible because i want to show this functionality in my
edit database using jsp and servlet
edit database
using jsp and servlet I am creating a website
using jsp and servlets that is used to view houses from a database. I want to be able to edit the information of each house. showAll.jsp shows all the houses and beside
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
Retail Point Of sales (using jsp and servlet)
Retail Point Of sales (
using jsp and
servlet) Hi, i want to know how to add products
using search module, i have a database and created a product... module. I dont have any background in java and
jsp, i dont have any idea where
upload a file and write it in JSP using servlet
upload a file and write it in
JSP using servlet Hello, I'm facing a problem here. I want to upload a file through abc.jsp and write the contents of file
using a
servlet in xyz.jsp. It is supposed to be a excel file which
fetch record from oracle database using jsp-servlet?
fetch record from oracle database
using jsp-
servlet? how can i fetch data from oracle database by
using jsp-
servlet. i'm
using eclipse, tomcat server and oracle database and creating
jsp pages and also
using servlet
get info from mysql using jsp and servlet
get info from mysql
using jsp and servlet HELLO! I wanna create a
jsp page which able to let me get its name, phone and other info by asking the user to key in their email address from mysql database by
using servlet and
jsp too
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
how to create bean using jsp and servlet
how to create bean
using jsp and servlet public class SampleBean... the following links:
http://www.roseindia.net/
jsp/usingbeansinjsp.shtml
http://www.roseindia.net/
jsp/loginbean.shtml
excel sheet reading and using that data - JSP-Servlet
excel sheet reading and
using that data i have to do a read a excel sheet file of a employee record and then i have to use a employee details to send mail to those employees how to do in
jsp sir please help me sir..
Thanks
Image upload in mysql database using jsp servlet
Image upload in mysql database
using jsp servlet Hello, I need code to insert image in mysql database, I have seen the code which is already in your portal but it is not inserting image into database it save in the folder