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.
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.
Joined: Dec 14, 2004
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 ]
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?