• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Saving session information on session timeout

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic