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

 
 


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

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 Pages:
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
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 <
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
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 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
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
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
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
need
need  i need a jsp coding based project plz help me
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
Sorting the array
Sorting the array  Implement a program to process votes for 5... who was in 3rd, 2nd and 1st place: Hints: ? you will need to declare 2 arrays... should initialise in the program ? you will need to modify the input method from
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
JSP Paging issue
JSP Paging issue  Hi; what should I have to write insted of "SQLCALCFOUND_ROWS" for MS-SQL database Amrit
Imp-HTML Table Sorting
Imp-HTML Table Sorting   Hi, I am working on a project and as per requirement from client i have to sort a table based on the ID Column. I want... of HTML table sorting where sorting happenes whenever user clicks on column header
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
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
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
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
Sorting Country names alphabetically
Sorting Country names alphabetically  Hello, I have a list of country names in an array. I need a jsp code which will sort me the country names in an alphaberical order. It would be more useful when I get the coding using
Array sorting - Java Beginners
Array sorting   Hello All. I need to sort one array based... need to sort the "name" array alphabetically. I can do that easily using... will be lost. I need to re-order the "name" array alphabetically so that my "type
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
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
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
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
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
Need E-Books - JSP-Servlet
Need E-Books  Please i need a E-Book tutorial on PHP please kindly send this to me thanks
Paging
Paging  How does paging in struts by use oracle database? Thanks in advance
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
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... to get a correct values... I m sending my jsp code also please help me sir
JSP - JSP-Servlet
-- Java JSP Servlet JavaScript...JSP  Hi ppl, I have a need , wherein I need to put in two options on a jsp , once the user selects one of them and clicks a button (say ok) then i
servlet code - JSP-Servlet
servlet code  how to implement paging or pagination in java code using servlets.  Hi Friend, Try the following code: import java.io.... of JSP page"); out.println(""); out.println(""); out.println
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
Java Servlet : Hidden Field
Java Servlet : Hidden Field In this tutorial, we will discuss about Hidden Field. It is one of session tracking technique. Hidden Field : Session... field as - <input type =?hidden? name =?name? value=?value?/> In servlet you
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
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
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 help on JAVA JSP
using JAVA JSP). e.g. Tool does not adjust with the resolution of the screen(need to know how I can adjust it based on the cureen system's screen resolution...Need help on JAVA JSP  Hi, I have never worked on java and I have
JSP combo and text field related questio
JSP combo and text field related questio  in JSP,i had stored data... and name given to that text field..but its showing somethimng null pointer exception..i need its solution with respect to JSP only..please help me out
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
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
Implement an interface in a JSP
Implement an interface in a JSP  Can we implement an interface in a JSP?   
JSP:HTML Form in-place Editing - JSP-Servlet
field that will save this ID and pass it on to the servlet and populate the data...JSP:HTML Form in-place Editing  I have an HTML form (form #1) which uses a java servlet to save customer data by entering values into textfield
java compilation error - JSP-Servlet
JSP, so please can u suggest me a way out.thanq very much for the previous answers, i need validations only using script but not struts2.  wat kind of validations you want to perform..........just search in google for form field
paging in php - Design concepts & design patterns
paging in php   comments() Name: Title: Description: i need paging code for this page sir by using only php for every page 10 records will printed. for every
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
how to make paging with class and ajax
how to make paging with class and ajax  paging with class and ajax... { print "<th>".mysql_field_name...; paging with class and ajax function showUser

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.