GeeCON Prague 2014*
The moose likes Servlets and the fly likes Page Refreshes and re-use of data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Page Refreshes and re-use of data" Watch "Page Refreshes and re-use of data" New topic
Author

Page Refreshes and re-use of data

Ron Olson
Greenhorn

Joined: Nov 22, 2000
Posts: 14
My question deals with the flow of data between Servlets and JSP pages, and how to deal with a page that is Refreshed by a user. My flow for a database-driven page is this:
1) Servlet accesses the database, bundles up the raw data needed for the eventual HTML creation, and stores that data in session variables.
2) Servlet redirects to a JSP page
3) JSP page takes the raw data from session variables, and creates the HTML from it.
But, what happens when the user refreshes that same page? It seems to me that I have two choices, each with pros and cons.
1) The JSP page can release the data once it's created its HTML. Then, when the servlet reruns, it starts from scratch, recreating the raw data from the database.
or
2) The JSP page can leave the data intact in the session variables. Then, when the servlet reruns, it can re-use the session data, thus avoiding recreating it from the database. But, in so doing it must somehow determine if this 2nd request is a repeat or something new, so that it will know if the saved data is usable or new data is needed.
Can anyone out there offer your experience on how best to deal with this?
Ron
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
Just have the JSP add a marker object to the session the first time it is used. If that object is there, this is not the first access.
if( session.getAttribute("used") == null ){
session.putAttribute("used","used");
}
else { // reused
}
------------------
author of:
 
jQuery in Action, 2nd edition
 
subject: Page Refreshes and re-use of data