Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to paginate rows of a table inside my servlet using hibernate.But once I click on the desire index of the page it always gives me only the first set of row of the table.

My servlet code:

Java
int pageIndex = 0;
int totalNumberOfRecords = 0;
int numberOfRecordsPerPage = 5;

String sPageIndex = request.getParameter("pageIndex");

if (sPageIndex == null) {
    pageIndex = 1;
} else {
    pageIndex = Integer.parseInt(sPageIndex);
}
int s = (pageIndex * numberOfRecordsPerPage) - numberOfRecordsPerPage;


List<ProductHasSize> phs = ses.createCriteria(ProductHasSize.class)
                .setFirstResult(s)
                .setMaxResults(numberOfRecordsPerPage)
                .list();

        for (ProductHasSize pro : phs) {... some html content here...}
        List<ProductHasSize> phs1 = ses.createCriteria(ProductHasSize.class)
            .setProjection(Projections.rowCount()).list();
    Iterator i = phs1.iterator();

    int noOfPages = 0;
        while (i.hasNext()) {
            Object o = i.next();
            totalNumberOfRecords += Integer.parseInt(o.toString());
           // System.out.println("totalNumberOfRecords - " + totalNumberOfRecords);
            noOfPages = totalNumberOfRecords / numberOfRecordsPerPage;
            //System.out.println("noOfPages - " + noOfPages);
            if (totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage)) {
                noOfPages = noOfPages + 1;
            }
        }

        for (int j = 1; j <= noOfPages; j++) {
            String myurl = "products.jsp?pageIndex=" + j;
            String active = j == pageIndex ? "active" : "";
            s2 = s2 + "<li class='" + active + "'><a href=" + myurl + ">" + j + "</a></li>";

        }


Thanks in advance.
Posted

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