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



Home Answers Viewqa JSP-Servlet Need to implement Paging and field based sorting in JSP Servlet

Have Programming Question? Ask it here!
 
 
 


Ashish
Need to implement Paging and field based sorting in JSP Servlet
2 Answer(s)      4 years and 10 months ago
Posted in : JSP-Servlet



Advertisement
View Answers

June 27, 2009 at 4:51 PM


Hi Friend

For pagination,try the following 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");
}
%>
<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>Class</td>
<td>Marks</td>
<td>Grade</td>
</tr>
<%
while(rs1.next()) {
%>
<tr>
<td><%=rs1.getInt("rollNo")%></td>
<td><%=rs1.getString("name")%></td>
<td><%=rs1.getString("class")%></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>


June 27, 2009 at 4:51 PM


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:
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 sorting on records. Following is my requirement: Suppose we fetch the record
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
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  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
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
Sorting a jsp page. - JSP-Servlet
Sorting a jsp page.  Hi there, It's me again. I tried to use Ajax... is process the code in the servlet. Here's the code in the jsp below: JSP Page Universal Books Home Page
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; what should I have to write insted of "SQLCALCFOUND_ROWS" for MS-SQL database Amrit
Sorting data in a jsp using a servlet while implementing comparable - JSP-Servlet
Sorting data in a jsp using a servlet while implementing comparable  ... at line 14 of the jsp code. I also want it to implement comparable to use... for displaying them in the jsp: 1. Universal Books Home Page 2. 3. 4
rich text field - JSP-Servlet
rich text field  Hi, this is jawahar. my question i need rich text field in my program . plz send me the code
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
How to implement session variables - JSP-Servlet
print it. Do I need to add any path like (@servlet) because how the jsp know...How to implement session variables  Hi, I have a servlet which gets parameter from a jsp page(x). it is stored in a variable in servlet as shown
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
need a jsp example - JSP-Servlet
need a jsp example  I need a jsp example to send a voice message or a audio .wav file from my pc to a mobile phone
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
Fetching database field from servlet to jsp page ?
Fetching database field from servlet to jsp page ?  Hello Java... field. I wanted to pass some of the database field from servlet to jsp... (i...) field to jsp page . { where 'rs' is Resultset object} please help
Paging
Paging  How does paging in struts by use oracle database? Thanks in advance
Need E-Books - JSP-Servlet
Need E-Books  Please i need a E-Book tutorial on PHP please kindly send this to me thanks
JSP:NEED RESPONSE ASAP PLEASE! - JSP-Servlet
JSP:NEED RESPONSE ASAP PLEASE!  Hi freind, If you could respond... and then sends this data in url to the next jsp page. The code works fine...://localhost:8080/examples/jsp/popup.jsp?name='+content1+'&&dd='+content2,'mywindow
JSP Paging Example in Datagrid
JSP Paging Example in Datagrid   ... to create paging in JSP. Two files are used "paging.jsp" and "... (); 6). Using  the Taglib to create paging and show records. Step:1
need to ans abt db - JSP-Servlet
need to ans abt db  helllo can you tell me m just starting to knowing abt servlet and i trying to making e-mail site so can you tell me with the java servlet wht is the backhand database to use to proper with that site
urgent need in two days - JSP-Servlet
the Given JSP Example Code-------------------------------Servletform.html<...; version="2.4"><servlet> <servlet-name>InsertDataExample</servlet-name> <servlet-class>DataInsertServletExam</servlet
javascript cal code for selecting date in text field - JSP-Servlet
javascript cal code for selecting date in text field  HI I want javascript calendar code to select date in text field in jsp.pls send me? .../jsp/emp-event.shtml http://www.roseindia.net/javascript/javascript
Implement an interface in a JSP
Implement an interface in a JSP  Can we implement an interface in a JSP?   
Need help in image uploading - JSP-Servlet
Need help in image uploading  hii, i have one doubt in uploading an image in jsp. I want to display image from DB to my jsp page. For that i... a folder named images in server but cant upload.Is any extra code need for uploading
Need to get the Data based on Date
Need to get the Data based on Date  Hie , I am having a criteria where i need to get the data based on date . I have written the query as "from Balance as b where b.date like ?" But the date field in DB is in datetime2 format
How to implement openId java web based application?
How to implement openId java web based application?  In my web application i want to implement the OpenId just like stackoverflow.com have to login.... Please tell me how to implement it in java web application in details
Need alternative for getParameter() method - JSP-Servlet
Need alternative for getParameter() method  Hi Expert, I would like to know if if any other way to fetch the vaule from text box withour using getParameter() method. Kindly requesting you to let me know it, If any way
Need Help in creating online quiz application using JSP - JSP-Servlet
Need Help in creating online quiz application using JSP  Hi, i am creating online Quiz application using JSP and MySQl ,Apache 6 in Netbeans IDE. i... the answer is correct or not,,but i can't implement the program further ,in such way
sorting
sorting   write a program to arrange sorting in rows,column and diagonal
Sorting
Sorting  can any help me know which sorting algorithm java uses for sorting collection and arrays
Re:Need Help for Editable Display table - JSP-Servlet
Re:Need Help for Editable Display table  Hi Genius i need a help in jsp to display editable display tag. I able to show the datagrid in jsp but i cant edit in that. My backend is with Mysql. I need to fetch the data
sorting
sorting  write to a program to arrange the sorting in rows, column and digonal
How automatically calculate age based on date of birth and current date using jsp and servlet?
How automatically calculate age based on date of birth and current date using jsp and servlet?  when user enters the Date of birth in one textbox then automatically age will be display on another textbox. event:Onlick Event
sorting
sorting  how to do sorting without using bubble sort,selection sort
Re:Need Help for Editable Display table - JSP-Servlet
Re:Need Help for Editable Display table  Hi Genius
Role based login ..Need the concepts.. - JavaMail
Role based login ..Need the concepts..  Role based login how can i do that.. Constraint: i should have only one jsp page for all the user... jsp page... want to know different ways of implementing the above
paging - Ajax
paging  i want sample code for paging using pager taglibs
Implement method in Jsp - JSP-Interview Questions
Implement method in Jsp   Hi Friends, where should we implement methods in jsp(In webApplication... of example related to jsp along with description, please visit: http
Need query for rollup based on month and category
Need query for rollup based on month and category   Hi, Here is my question.. I have a table as below. trans_date,cateory,amount 1-1-11,1,10 2-1-11,2,20 3-2-11,3,15 23-2-11,4,12 13-5-11,4,19 Need a query to get result like
i want to do a project using jsp and servlet....What are all the materials i need to study
i want to do a project using jsp and servlet....What are all the materials i need to study   i want to do a project using jsp and servlet....What are all the materials i need to study ?   Please visit the following
sorting
sorting  I have got 3 questions in interview . please help me how to do ? 1) public int rotatedSearch(int[] values, int start , int end , int x) { //implement a search function for a sorted rotated array. //duplicates
jsp servlet
jsp servlet  i dont know how to write a code to Create a JSP with one text field to enter the URL and a submit button.On clicking the submit button, send the request to a servlet .Once the servlet receives the request, it need
jsp servlet
jsp servlet  i dont know how to write a code to Create a JSP with one text field to enter the URL and a submit button.On clicking the submit button, send the request to a servlet .Once the servlet receives the request, it need
JSP - JSP-Servlet
JSP & Servlet Example Code  Need example of JSP & Servlet
urgent need for jsp code with mysql query for uploading and downloading file - JSP-Servlet
urgent need for jsp code with mysql query for uploading and downloading file  can anyone tell me how to upload and download a doc file using jsp with mysql as the back end... and kindly help me to create the table too
jsp servlet
code to Create a JSP with one text field to enter the URL and a submit button  i dont know how to write a code to Create a JSP with one text field... the request to a servlet .Once the servlet receives the request, it need to create
jsp servlet
how to write a code to Create a JSP with one text field to enter the URL  i dont know how to write a code to Create a JSP with one text field... to a servlet .Once the servlet receives the request, it need to create a thread
Selection based on other selection in jsp
. I want to populate the field on product_name based on the category name...Selection based on other selection in jsp  I am trying to create a jsp page. Where I am having a select list containing category name and for each
 

 

 

DMCA.com