Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Development-process Paging or pagination
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


harini
Paging or pagination
2 Answer(s)      4 years and 7 months ago
Posted in : Development process


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...

i done this but i m geting starting 10 records correctly but i m unable to get a next 10 records it is showing a starting 10 records only please help me to get a correct values...
I m sending my jsp code also please help me sir.....


//Pagination.jsp

<%@ page import="com.antares.servlet.EmployeeControllerServlet"
contentType="text/html; charset=ISO-8859-1"%>
<%@ page import="java.util.*"%>
<%@ page import="com.antares.DAO.*"%>
<%@ page import="com.antares.*"%>
<%@page import="java.util.Iterator"%>
<%@page import="com.antares.DTO.Employee"%>
<%@ page import="java.util.*,java.sql.*,sun.jdbc.*"%>


<%
int intEmpId = 0, intEmpPhoneNo = 0;
String strFstName = null, strLstName = null, strEmpAdd = null, strEmailId = null, strStatus = null;
ArrayList<Employee> arlEmp = null;
String startIndexString = request.getParameter("startIndex");
String strValue=request.getParameter("value");
if (strValue == null) {
strValue = "0";
}

int value = Integer.parseInt(strValue);
System.out.println("Starting index is "+value);

if (startIndexString == null) {
startIndexString = "1";
}

int startIndex = Integer.parseInt(startIndexString);
System.out.println("Starting index is "+startIndex);

%>

<html>
<head>
<title>Pagination</title>
</head>

<body bgcolor="#ffffee" text="blue">
<h3>The Following Are List Of Employees...</h3>
<form target="_top" name="viewForm" method="post">
<%
int numPages = 0;
%> <%
String columnName = "", strInitialVal = null;
int count = 0;
int totalCols = 0;
int increment = 0;
int numRows =0;
final int numRecordsPerPage = 5;
int initial = 0;
int temp = 0,k=0;
System.out.println("initial value is " + initial);


%>
<table border="2" align="center" width="100%">
<tr>
<td>Index</td>
<td>EmpId</td>
<td>FirstName</td>
<td>LastName</td>
<td>Address</td>
<td>PhoneNumber</td>
<td>EmailId</td>
<td>Status</td>
</tr>
<%
System.out.println("initial value is " + initial);
if (session.getAttribute("employeeDataBase") != null) {
arlEmp = (ArrayList) session
.getAttribute("employeeDataBase");
}
numRows = arlEmp.size();

System.out.println(" total no. of records : " + numRows);

System.out.println(" Num of Records per page : "
+ numRecordsPerPage + "\n");

numPages = numRows / numRecordsPerPage;

System.out.println(" \n no. of pages before if Statement : "
+ numPages);

int remain = numRows % numRecordsPerPage;

if (remain != 0) {

numPages = numPages + 1;

}

System.out.println(" \n no. of pages after if Statement : "
+ numPages);
startIndex = numRecordsPerPage;

if ((numRecordsPerPage) <= numRows) {

increment = numRecordsPerPage;
System.out
.println(" \n 1. no. of pages (increment)in if condition is : "
+ increment);
} else {
if (remain == 0) {

increment = numRecordsPerPage;
System.out
.println(" \n 2 no. of (increment) pages in if condition is : "
+ increment);

} else {
startIndex = remain;
increment = remain;
}
}

System.out.println("ArrayList size is "+arlEmp.size());
System.out.println(".........value of first array list is "+arlEmp.get(1));
temp=value+numRecordsPerPage;
k=value;
if (arlEmp != null && arlEmp.size() > 0 && value<arlEmp.size()) {
Iterator<Employee> iter = arlEmp.iterator();
while (iter.hasNext() && initial < numRecordsPerPage) {
if(arlEmp.get(k)!=arlEmp.get(temp)){

Employee empDTO = (Employee) iter.next();
intEmpId = empDTO.getIntEmpid();
strFstName = empDTO.getEmp_fristName();
strLstName = empDTO.getEmp_lastName();
strEmpAdd = empDTO.getEmp_address();
intEmpPhoneNo = empDTO.getEmp_phone();
strEmailId = empDTO.getEmp_email();
strStatus = empDTO.getEmp_status();
System.out.println();
value++;
k++;

System.out.println("initial value is= " + initial);
%>

<tr>
<td><%=value%></td>
<td><%=intEmpId%></td>
<td><%=strFstName%></td>
<td><%=strLstName%></td>
<td><%=strEmpAdd%></td>
<td><%=intEmpPhoneNo%></td>
<td><%=strEmailId%></td>
<td><%=strStatus%></td>
</tr>

<%
initial++;

}
}
}
temp = initial;
System.out.println("Initial value is: " + initial);
%>

<%


for (count = startIndex; count < increment; count++) {
%><tr>
<%
for (int i = 1; i <= totalCols; i++) {
%><td><%=arlEmp.get(count - 1)%></td>
<%
System.out.println("**** " + arlEmp.get(count - 1));

}
System.out.println("out of inner for loop..");
System.out.println("**** " + arlEmp.get(count - 1));
%>
</tr>

<%
}
System.out.println("out of outer for loop..");
%>



</table>
<br>
<br>
<br>



<table width="100%">
<tr>

Displaying Records:

<%
startIndex = numRecordsPerPage;
if (numRecordsPerPage < numRows) {
%>
<%=(value - increment)+1%>
-
<%=value%>
<%
} else {
%>
<%=value%>

<%=-numRows%>
<%
}
%>

<%
for (int i = 1; i < numPages; i++) {

if (i==value) {
System.out.println("page value is "+i);
//startIndex--;
System.out.println("Initial value is " + initial);
}
else{
%>
<a href="Pagination.jsp?value=<%=value%>"><%=i%> </a>
<%
}
}
increment = numRecordsPerPage;
startIndex = numRecordsPerPage;
if (numRecordsPerPage<=numRows) {
%>

<a href="Pagination.jsp?value=<%=value%>">Next</a>

<%
}
%>
</tr>

<a href="Pagination.jsp">Previous</a>

</table>
<br>
<br>
<a href='../html/login.html'>
<h3>Click Here To Go Home Page</h3>
</a> <br>
</form>
</body>
</html>

thanks in advance

Regards
harini

Advertisement
View Answers

April 1, 2010 at 11:13 AM


Hi Friend,

We are sending you our code.

<%@ 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");
System.out.println(totalRows);
}
%>
<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>


April 1, 2010 at 11:14 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();
}
%>

Thanks


Related Tutorials/Questions & Answers:
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...("Starting index is "+startIndex); %> Pagination The Following
Paging
Paging  How does paging in struts by use oracle database? Thanks in advance
Advertisements
Pagination
Pagination  How to apply pagination in JSP. Please help
Pagination
Pagination  How to implement pagination in struts using hibernate
Pagination
Pagination  How to create pagination in jsp with EJB using MS SQL
pagination
pagination  How to set pagination using java script to display data
pagination
pagination  please let me know how to fetch records from database and display them using paging in jsp and struts
pagination
pagination  I need to give pagination with where condition (query.......   Here is a simple jsp pagination code where we have used mysql database...()) { totalRows=rs2.getInt("cnt"); } %> <html> <h3>Pagination of JSP
Pagination
Pagination  I want to write one where condition in sql query in pagination concept
Pagination
Pagination  Is their plug n play approach for creating pagination? 1) Server side as well as client side
pagination
pagination   Simple way for pagination in jsp using java script to display data without send database
Pagination
Pagination  I want to know how to use pagination in jsp. I have a list object from that i want to display 3 questions per page can u help me
pagination
pagination  I need to give pagination with where condition (query="select * from qtn where qid='"+replyQuesionId+"'limit "+iPageNo+","+showRows+"" ) like this I want, without where condition it is working but with condition
Pagination
=rs2.getInt("cnt"); } %> Pagination of JSP page Terr_Code Terr_Name
pagination
code of pagination. In the given code,we have taken the database table student...;Pagination of JSP page</h3> <body> <form> <input type="hidden
Pagination
Pagination  pagination in jsp   Hi Friend, Try the following code: <%@ page language="java" %> <%@ page import="java.sql.*" %>...()) { totalRows=rs2.getInt("cnt"); } %> <html> <h3>Pagination of JSP
pagination
pagination  how can i start pagination in jsp?   Hi Friend, Try the following code: <%@ page language="java" %> <%@ page import...;Pagination of JSP page</h3> <body> <form> <input type="hidden
paging - Ajax
paging  i want sample code for paging using pager taglibs
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
Pagination in PHP code - PHP
Pagination in PHP code  3.Pagination in PHP codeIs it possible to get the paging in PHP without database call? How will I show the image instead of numbering? any code that can help
Php Sql Paging OR Pagination
Php Sql Paging OR Pagination       This application illustrates how to create a pagination.... Displaying all data in one page in not a good idea. For this situation, pagination
how to make paging with class and ajax
how to make paging with class and ajax  paging with class and ajax
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
JSP Paging issue
JSP Paging issue  Hi; what should I have to write insted of "SQLCALCFOUND_ROWS" for MS-SQL database Amrit
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
Pagination in java
Pagination in java  How to handle pagination when there are records like say in millions in java? Or do we handle this using SQL? I as asked... far good performance then Java pagination. Thanks
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
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
pagination in jsf
pagination in jsf  Hi , i am implemeting an online exam system , i have placed my questions as an arraylist in my jsp file within tag , but now i want to paginate these questions based on respective questions by clicking
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
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
 

 

 

DMCA.com