aspose file tools*
The moose likes Servlets and the fly likes Saving session information on session timeout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Saving session information on session timeout" Watch "Saving session information on session timeout" New topic
Author

Saving session information on session timeout

Rick Beaver
Ranch Hand

Joined: Dec 14, 2004
Posts: 464
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


ph34r my 133t j4v4 h4><0r1ng sk177z
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
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

Joined: Dec 14, 2004
Posts: 464
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

Joined: Dec 11, 2004
Posts: 13410

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.


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

Joined: Dec 14, 2004
Posts: 464
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

Joined: Dec 11, 2004
Posts: 13410

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
 
subject: Saving session information on session timeout