aspose file tools*
The moose likes JSP and the fly likes Please help with pagination in JSP? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Please help with pagination in JSP?" Watch "Please help with pagination in JSP?" New topic
Author

Please help with pagination in JSP?

jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
I have an ArrayList contain data return from database. The list already sorted. I want to walk thru this list and display 10 record per page with NEXT , PREVIOUS link.

the code in using JSP embedded with JAVA scriplet <% %>, so there is no special taglib.

I have search thru the posts but could not find good sample code. Can any one please help with this. Thank in advance


Many thanks in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

It would be best to let the DB do the sorting and paging. Please read the JSP FAQ entry on this topic.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
I read the FAQ and i don't think it is the efficient approach for my case, since my data is collecting from different source, and in the end i stuff into the one Object as ArrrayList.

so any sample code suggestion , thank in advance
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by jay lai:
since my data is collecting from different source,


Can you explain, a little further, what you mean by 'different source'?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Basically is not coming from the same query, and also for now I don't want to touch the query b/c somebody else may invoke it.
So on the jsp now I got the whole Array List of data sorted, I just need some sample code that do the paging using scriplet in JSP.

Thnks in advance
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

JSTL's forEach tag has begin and end attributes.
In your form or hyperlinks, send the desired items from the list.

You will either need to store the whole list in session which could result in a lot of memory consumption if the results are large or, read the entire result from the database with each call and ignore all but the rows that you want to show. This can also be inefficient if the database is returning large result sets.
Pallavi Srivastava
Ranch Hand

Joined: Dec 22, 2006
Posts: 38
Hi,

This is the code for pagination.It will display number of records ,previous-next link,page number and can also customise number of records displayed on screen.



You have to customise according to your requirement.
Hope this helps.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Good lord, what a mess.

Regardless of how you are obtaining the data, the paging should not be happening in the UI layer. Rather your model should have an API that allows the UI to ask for the records for a particular page, which should return only the relevant records.

This not only keeps your UI code "cleaner", but also allows you to change the details at any later date on how the page of data is obtained.

Doing this sort of thing in the UI is a poor practice and should be aoivded.
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Thank you Pallavi Srivastava;
I think that what I am looking for and try to customize it.
Thanks again.
Pallavi Srivastava
Ranch Hand

Joined: Dec 22, 2006
Posts: 38
Thank you Bear for the tip.I am able to develop logic of the application but still weak at architect level.As you said, I will make a pagination component and remove the mess from front end.

Thanks again.
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
That's is a good suggestion from Bear. But sometimes, the application is not in the new redesign mode, sometimes you need to modified the OLD code therefore, there is nothing much in design or architech that you can change in the mean time while time is in constrant.

thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Ah yes, how many times have I heard management say that? And while it may appear to be a logical conclusion, frequently it is not. Sometimes you'll spend a far greater amount of time trying to get a poorly constructed system to work the way you want it to than it would take to refactor the whole thing.

Management tends to perfer the former though because, even if it is buggy, they can see something, which gives them the illusion of progress. Whereas a refactor gives them the feeling that nothing is happening.

But this probably belongs in the process forum...
[ June 08, 2007: Message edited by: Bear Bibeault ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help with pagination in JSP?