Sunday, October 12, 2008

How to implement Pagination in JSP Pages?

This question was asked by one of our visitors, Ranvijay. Thank you Ranvijay for posting it. I thought it's worth posting the response as a separate article to improve the chances of it reaching to a wider range of visitors.

How to implement Pagination in JSP?

I'm afraid there isn't a standard way of implementing pagination. Different situations may demand diffrent strategies to be followed. If you don't have huge data (result set) for a query you're not very much concerned about the scalability and memory (which is very unusual in a typical web applications) then you may like to read the entire data into a bean in the middleware and from that bean you can build the different pages. You just need to iterate through the master bean to get he data for a particular page depending upon its page number and maybe you can either populate a temp page bean with that data or you may directly populate the view fields. This approach has the obvious advantage of minimizing the DB trips. Now, it's quite easy to understand that evn if you've sufficient memory but if you have huge data then the first query will take too much time to get all the data back which may be frustrating for the end-users. So as already said this approach should be considered only in the cases where you're sure that a query won't result into too much of data to be returned.

If the query returns (or may return) huge data and if you're concerned about the scalability and memory requirements then you'll probably need to make more than one DB calls (which will eventually slow down the overall performance). You need to pass-on either the page number directly or some other computed data so that you can pick only those many records which you need for that page - maybe by putting appropriate values in the ORDER BY clause of the DB query. I hope this makes some sense :-)

To implement the view of the pagination you will probably need to associate all the page numbers with some servlet accepting a parameter identifying which page number it was called from. For example: you may associate page 1 with <a href="/PaginationServlet?pageNo=1>1<a>

How to find how many page numbers to display at the bottom/top? Well... first fire a query which returns you the count of the records and then based on this count you can easily calculate the page numbers. This approach has another advantage of mixing the above two approaches based on what he count is. That means if the count is low (which can be accomocdated in 4-5 pages) then you'll probably prefer to get all the data in one DB trip only otherwise get data in chunks and prepare the pages from those chunks.

You would probably like to create separate temp beans in the middle tier for at least few pages (for many pages is again a situation depndent thing) so that if sombody clicks on already visited page then you have a chance of geting the already populated temp bean (instead of creating that every time that link is clicked). This will ensure the end users get a faster display of already visited pages.

If you're using Hibernate then you may like to have a look at the Criterion and Query interfaces.


1 comment:

JatinAneja said...

Jmesa is a good tool to implement pagination. Along with this it provides sorting, filtering, export (csv,xls,pdf) feature.
It is a free to use tool. And very easy to setup.
More can be learnt on link :