• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using request scope during pagination

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I need to paginate the data in my jsp.

Currently i populate a bean -> place it in session -> redirect to jsp page -> display data from bean -> remove the bean from session in the jsp page.

Initially i wanted to use the request scope for the bean. But since i redirect to the jsp, i think this is not possible. So, if i need to use request scope to store the bean, i have to use forward instead of redirect.
Am i right, or is there something i am missing?

Also i need to know if there is any work around instead of using sessions, and manually managing the bean? I dont want the bean to be in the session if anything goes wrong and the bean is not removed(as i feel in case of request, the bean will be removed by the container), because the data in the bean can be a bit heavy and should not be in the session longer than it is required.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are generally two methods employed when dealing with pagination in a web page:

1-Get all the records from the database and store them in an in-memory data structure. When the user requests the next page, get the page from the stored data structure. Advantage: it's faster because it only gets data from the database once. Disadvantage: it takes up more memory, and if the data set is large enough could even cause an "out of memory" error.

2-Get a total number of records from the database so the user knows how many pages there are, but only display the first page, and don't create any type of data structure to cache the data. When the user requests the next page, read the data from the datbase again, this time only displaying the second page. Advantage: It can handle data sets of any size without worrying about putting too much data in memory. Disadvantage: It may be slower because it access the database for each page displayed.

If you use method 1 above, you cannot use the request. End of conversation. You have to have someplace to keep this in-memory data structure, and the request is not an option because it does not persist over multiple requests to the server. The HTTPSession is your best option for storing the in-memory data structure.

If you use method 2, you can use the request as long as you have hidden fields in the page to store things like total pages, current page number, records per page, etc.
 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My solution would be to put the form in request scope and forward to the appropriate jsp. The downside is that when the user clicks any of pagination functions, you will have to repopulate the bean again to fill it with the data for that page. Passing a limit and offset would make the pagination stuff pretty efficient, and is compatible with most databases (if that is where you data is coming from).

Many people would prefer to shove all of the results into a bean in session scope because it is easy to work with, but too many session objects will make the site very unscalable. Especially if this bean has so many results that you have to paginate them.

Just my 2 cents.

Are you setting redirect to true in your action? If so, why?
 
azhar bharat
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clarifying.

I need to make sure on one more issue.

If I am saving a bean in request, request.setAttribute("RequestBean",bean), will this bean be destroyed by the container once the response is finished?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's right. A better way to put it would be that it is out of scope and eligible for garbage collection once a response to the request has been sent back to the browser. When it actually gets destroyed is up to the JVM.
 
Get out of my mind! Look! A tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic