servlet code



Share on Google+Share on Google+

servlet code
2 Answer(s)      7 years ago
Posted in : JSP-Servlet
how to implement paging or pagination in java code using servlets.
View Answers

March 23, 2010 at 5:37 PM

Hi Friend,

Try the following code:

import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PaginationInServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
PrintWriter out = response.getWriter();
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root";, "root");
ResultSet rs1 = null;
ResultSet rs2 = null;
PreparedStatement ps1=null;
PreparedStatement ps2=null;

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;
String query1="SELECT SQL_CALC_FOUND_ROWS * FROM student limit "+iPageNo+","+showRows+"";
String query2="SELECT FOUND_ROWS() as cnt";
if( {
out.println("<html><h3>Pagination of JSP page</h3><body><form>");
out.println("<input type='hidden' name='iPageNo' value='<%=iPageNo%>'>");
out.println("<input type='hidden' name='cPageNo' value='<%=cPageNo%>'>");
out.println("<input type='hidden' name='showRows' value='<%=showRows%>'>");
out.println("<table width='100%' cellpadding='0' cellspacing='0' border='1' >");
out.println("<tr><td>Roll No</td><td>Name</td><td>Marks</td><td>Grade</td></tr>");
if(totalRows<(iPageNo+showRows)) {
catch(Exception e){
out.println("<tr><td colspan='3'><div>");
int i=0;
int cPage=0;
if(totalRows!=0) {
int prePageNo=(cPage*totalRecords)-((totalRecords-1)+totalRecords);
out.println("<a href=PaginationInServlet?iPageNo="+prePageNo+"&cPageNo="+prePageNo+"'> << Previous</a>");
out.println("<a href=PaginationInServlet?iPageNo="+i+"style=cursor:pointer;color: red><b>"+i+"</b></a>");
else if(i<=totalPages){
out.println("<a href=PaginationInServlet?iPageNo="+i+">"+i+"</a>");
if(totalPages>totalRecords && i<totalPages){
out.println("<a href=PaginationInServlet?iPageNo="+i+"&cPageNo="+i+"> >> Next</a>");
out.println("<b>Rows "+startResult+"-"+endResult+"Total Rows"+totalRows+"</b></div></td></tr></table></form></body></html>");

March 23, 2010 at 5:38 PM



catch(Exception e)
catch(Exception ex){}
public int nullIntconvert(String str){
int num=0;
if(str==null) {
else if((str.trim()).equals("null")) {
else if(str.equals("")) {
catch(Exception e) { }
return num;

For the above code, we have used following database table:

CREATE TABLE `student` (
`rollNo` bigint(40) NOT NULL auto_increment,
`name` varchar(40) default NULL,
`marks` varchar(40) default NULL,
`grade` varchar(40) default NULL,
PRIMARY KEY (`rollNo`)



Related Tutorials/Questions & Answers:
servlet code
servlet code  how to implement insert update n delete in one servlet in net beans   If you want to perform insert, update and delete functions in the same servlet, then please visit the following links: http
servlet code - JSP-Servlet
servlet code  Create a servlet to develop a login application with javascript clientside validations and serverside validations
servlet code - JSP-Servlet
servlet code  how to implement paging or pagination in java code using servlets.  Hi Friend, Try the following code: import code, we have used following database table: CREATE TABLE `student
Searching for Code - JSP-Servlet
JSP, Servlet Searching for Code  Hi, i am looking for a jsp servlet code examples for the search function in my application.Thanks
code - Servlet Interview Questions
by the servlet container. The status code of the response always should...code  servlet service method along with an example code  Hi Friend, Servlet service() method: Once the servlet starts getting
atm code in servlet& jsp
atm code in servlet& jsp  pls send me the code of ATM project in servlet jsp . my requirements are first of all an user login window open then balance enquiry, withdraw of money, money transfer, then log out. pls send as early
servlet code to update password?
servlet code to update password?  Create a servlet page which helps the user to update his password. The user should give the username,old password... users.then Store it into a table. please tell me the code for the above question
code required in servlet
code required in servlet  hello... you provided me the answer with javascript.. i want to check which button is clicked in the servlet.. i have only 1 servlet an in that separate codes for different button clicks.. can u plzzz
servlet code problem - JSP-Servlet
servlet code problem  This is my JSP code index.jsp Sync... here!!!..., i want to pass the texbox value and file to servlet im stuck with this error plzz help me..., in servlet if i use request.getParameter() its
jsp code - JSP-Servlet
jsp code  Can anyone help me in writing jsp/servlet code to retrieve files and display in the browser from a given directory.  Hi Friend, Try the following code: Thanks
Encryption code - JSP-Servlet
Encryption code  Iam developing a web security application for that reason i need a code of encryption in servlet. Can u please send it to me.Its very urgent Because my project delivery date is very near
Help in completing Servlet code!
Help in completing Servlet code!  Complete the following Servlet codes to store the information read from the input form to session object. public... String comment = req.getParameter(?commentText?); //Complete the code to store
servlet code problem - JSP-Servlet
servlet code problem  This is my JSP code index.jsp Sync Data Sync Data Please use the following input box to upload file or enter... to servlet im stuck with this error plzz help me..., thanks in advance....  
code for JSP and Servlet - JSP-Servlet
code for JSP and Servlet  i have to create a jsp page that contains username and password, so how to code servlet according to it?  Hi sushil, package javacode; import*; import javax.servlet.*; import
servlet code - Applet
servlet code  how to communicate between applet and servlet  Hi Friend, We are providing you the code that will display the message sent from the servlet to applet. Here is the code of '
Code - JSP-Servlet
Code  Using servlet,JSP,JDBC and XML create a Web application for a courrier company to provide online help in tracking the delivery status of items
java servlet code - Servlet Interview Questions
java servlet code  iam doing a project in java, preparation of on line examination in that how to prepare a question paper using servlet code
code - JSP-Servlet
; use some server side script like servlet or jsp, call these when user click
download code using servlet - Servlet Interview Questions
download code using servlet  How to download a file from web to our system using Servlet
code - Servlet Interview Questions
code  code for matrimoney site using servlets
source code - JSP-Servlet
source code for webcam chat application  I want source code for this webcam chat application. I like the concept of XAWARE ORG regarding web cam chat. So that i want the code through this XAWARE only.pls send the code as soon
java code - JSP-Servlet
java code  Code to send SMS through mobile to a web application
jsp code - JSP-Servlet
jsp code  I need code for bar charts using jsp. I searched some code but they are contain some of their own packages. Please give me asimple code... friend, Code to solve the problem : Thanks
program code - JSP-Servlet
program code  what is the program code for LOGINFORM USING SERVLETS? RESPOND SOON
jsp code - JSP-Servlet
jsp code  sample code for change password example Old Password: new Password: confirm Password
program code - JSP-Servlet
program code  what is the program code for LOGINFORM USING SERVLETS? RESPOND SOON
jsp code - JSP-Servlet
jsp code  sample code to create hyperlink within hyperlink example: reservation: train: A/C department non A/c Department
Code Works - JSP-Servlet
Code Works  Hi The code provided is working fine along with the pagination . i edited the queries and that makes difference.. here is the code. Thank you Regards Eswaramoorthy Pagination of JSP page
code - JSP-Servlet
code  hi sir my question is i have created a html ang jsp code.... how to write the code to accept an existing id and password.  Hi Friend, Try the following code: 1)login.html: Login
code for this problem - JSP-Servlet
code for this problem   i have buttom submit .It is working with single click.Suppose if i am At the same time clicking(parallel) twise second request will be stoped until process the proces first requst and terminate
source code - JSP-Servlet
source code  please i want source code for online examination project (web technologies)  Hi Friend, Send some details of your project. Thanks