File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Saving session information on session timeout

 
Rick Beaver
Ranch Hand
Posts: 464
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers

I am storing application preferences in a HttpSession - for example session.setAttribute("width", "20")

I only want these to be stored in memory until the session finishes (through logout, browser closing or session timeout) at which time I then want to write these values out to persistant storage.

My question is, is there a way for me to ensure attributes stored in the session are written to persistant storage prior to the session dissapearing? It needs to be automatic and hidden and account for the possibility of the user not cleanly logging out.

Thanks for any advice.

Nick
 
K Riaz
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not simply write a servlet which retrieves all the values from the session (or hidden input parameters) and then save them to a properties file? Then when you need to retrieve them again, simply read it from the properties and, if the client needs it, store it in session variables from the servlet.
 
Rick Beaver
Ranch Hand
Posts: 464
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kashif

Thanks for the suggestion. My original approach was to write the values out to a file everytime they change, unfortunately with the amount of changes made to the values and the amount of users it is not viable to update the persistant storage everytime a value changes.

I need to store the values in the session so that they can be changed easily throughout the session with no IO overhead. The values only need to be stored when the session is destroyed so that they can be retrieved the next time the user logs in.

Thanks again
[ February 16, 2005: Message edited by: Rick Beaver ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick,

I believe the HttpSessionListener is exactly what you're looking for:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSessionListener.html
Just put your persistence code into the sessionDestroyed method.

This has been around since Servlet Spec 2.3 so you will need to check with your server's documentation to see what version it supports.
 
Rick Beaver
Ranch Hand
Posts: 464
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
Nick,

I believe the HttpSessionListener is exactly what you're looking for:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSessionListener.html
Just put your persistence code into the sessionDestroyed method.

This has been around since Servlet Spec 2.3 so you will need to check with your server's documentation to see what version it supports.


Perfect Ben thanks - it is working fine.

I have encountered a problem though.

The sessionDestroyed method of HttpSessionListener is only called when session.invalidate is called. It is not called if the user navigates away from the page, closes their browser etc. This is fine if the session will eventually time out on it's own - is this the case?

Thanks again
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is true.
In a stateless environment, the server has no way of knowing if the user browses to another page, looses their connection, or has Windows/MSIE crash for them.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic