Pagination in JSP/Java

December 13th, 20074 Comments

Here i’m going to write, how to do pagination in Java. The situation is first count how many rows in the database table, then query the database by page size (how many rows you want to display on a web page).

In Jsp

String pno=request.getParameter("pno"); // this will be coming from url
int pageno=0;
if(pno==null)
{
pageno=1;
}
else
{
pageno=Integer.parseInt(pno);
}
int next=pageno+1;
int previous=pageno-1;

//Java method 
ArrayList al=<strong>object.getData(pageno);</strong>

// do all your showing data logic here

//print the previous and next links
if(previous!=0){
Previous link (url.jsp?pno=previous)
}

int max=<strong>obj.getTotalRows()</strong>; // total number of rows from database table

int check=(next-1)*10; // each page 10 rows

if(check&lt;max){  // print next link if check is less than total rows
Next link (url.jsp?pno=next)
}

In Java

//for getting the data according to page number
public ArrayList <strong>getData(int pagenumber)</strong>
{
  ArrayList al=new ArrayList();
  int offset = (pagenumber - 1) * 10;
  
 //write all the logic and query data with the limit offset

  Query + limit offset ,10;

 return al;
}

//for getting total rows

public int <strong>getTotalRows()</strong> {
//write your logic to get total count of the rows
 Query total rows from db table

return total_rows;
} 

Related Posts:

Tagged : , , ,

4 Responses

  1. Hi dude, i need the complete code … I use JSP/Servlet to do pagination and MySQL as my backend… Please help me regarding this….

  2. Nothing happend

  3. pno is undefined whats that is that a hidden feild how will the value of pno change?????such that the page number changes……can u please give the complete code

  4. Hello zaryaar,

    Here pno is not a hidden field, but the page number attached to the URL
    eg. url.jsp?pno=1, so u will get the pno from request.getParameter

    I hope this may help you.