Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
@WebServlet("/PagenationServlet") 
public class PagenationServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();

    int  pageNo=Integer.parseInt(request.getParameter("pageNo"));
    int startIndex=pageNo*3-3;

    IEmpDao dao=new EmpDaoImpl();
    List lt=dao.readEmps(startIndex);

    out.println("<center> ");
    out.println("");
    out.println("");
    out.println("");
    Iterator it=lt.iterator();
    while(it.hasNext())
    {
        Employee e=(Employee)it.next();
        out.println("");
        out.println("");
        out.println("");
        out.println("");
        out.println("");

    }
    out.println("<table border="1"><tbody><tr><th>EMPNO</th> <th>ENAME</th> <th>SAL</th> <th>DEPTNO</th></tr><tr><td>"+e.getEmpNo()+"</td><td>"+e.getEName()+"</td><td>"+e.getSalary());
        out.println("</td><td>"+e.getDeptNum()+"</td></tr></tbody></table>");

    long totalRows =dao.getNoOfRows();
    long noOfPages =totalRows/3;
    if(totalRows%3!=0)
    {
        noOfPages++;
    }
    if(pageNo==1)
    {
        int x=pageNo+1;
        out.println("<a href=./PagenationServlet?pageNo="+x+">  Next");
    }
    else if(pageNo==noOfPages)
    {
        int x=pageNo-1;
        out.println("<a href=./PagenationServlet?pageNo="+x+">  Prev");
    }
    else
    {
        int x=pageNo+1;
        out.println("<a href=./PagenationServlet?pageNo="+x+"> Next");
        int y=pageNo+1;
        out.println("<a href=./PagenationServlet?pageNo="+y+"> Next");

    }
}
}


What I have tried:

I'm trying to do a program related to pagination concept using Servlet-HibernateIntegration. but when I'm executing the program I'm getting the error called NumberFormatException
Posted
Updated 17-Feb-18 4:51am
v3
Comments
Richard Deeming 16-Feb-18 12:51pm    
You haven't told us, but I'm guessing it happens on the line:
int  pageNo=Integer.parseInt(request.getParameter("pageNo"));
You've made two assumptions there: 1) the request contains a parameter called "pageNo"; and 2) the value of that parameter can be converted to an integer.
Member 13681858 17-Feb-18 1:05am    
i'm trying to print 3 records from database per page.for that i'm converting
ThilinaMD 16-Feb-18 13:05pm    
try Hide   Copy Code
int pageNo=Integer.parseInt(request.getParameter("pageNo").toString());
Member 13681858 17-Feb-18 1:15am    
@ThilinaMD i tried with your code.i'm getting same exception.
i have a table in database with name emp1. I've 11 records in that. i want to print 3 records in each page, in last page 2 records i want to print while converting the pageNo variable it is throwing NumberFormaatException
ThilinaMD 17-Feb-18 2:21am    
So at first when you run the servlet there is no value for pageNo. Try to set value for (using if condition) page no variable at first.

1 solution

try this code
int pageNo = 1;
if (request.getParameter("pageNo") != null) {
    pageNo = Integer.parseInt(request.getParameter("pageNo"));
}
instead of
int  pageNo=Integer.parseInt(request.getParameter("pageNo"));

Error is caused due to at first when your servlet load there is no value for pageNo varialble
 
Share this answer
 
v2
Comments
Member 13681858 19-Feb-18 0:18am    
@ThilinaMD thank you for helping me for this..
program is executed
ThilinaMD 19-Feb-18 6:39am    
Welcome and happy coding...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900