We know we can store and retrieve data in HTTP Session. Is there any out of the box way to store the data in the Session, that will be automatically destroyed after a certain period of time (while the Session still stays and keeps other data stored)?
-
possible duplicate of [Session timeout in Java EE](http://stackoverflow.com/questions/4958155/session-timeout-in-java-ee) – Robert Moskal May 25 '15 at 01:50
-
Read the question carefully: "while the Session still stays and keeps other data stored" – siva636 May 25 '15 at 02:02
-
Ah so you want a different timeout for different values in your session. I didn't get that from my first reading. – Robert Moskal May 25 '15 at 02:05
-
1One way to achieve this is to invalidate the session after the specified time and create a new session immediately afterwards. While doing so you could copy only the data that needs to continue in the session. Other data will be destroyed with the old session. – ramp May 25 '15 at 05:40
1 Answers
You won't get this for free. If I were needed this feature and had a spring based application, then I might create a session based bean that wrapped some simple cache. Being lazy, I might use the guava cache:
https://code.google.com/p/guava-libraries/wiki/CachesExplained
If I wasn't using spring, I would do the same, perhaps putting the cache on the ServletContext and making sure that the cache key was partially comprised by some identifier for the current user (like the session id).
You could even put the guava, or other cache directly on the user session (you might do that when the session is created). Your access method is always going to feel a little different than accessing your naked session.
Depending on your servlet container, you could go so far as replace the session implementation with your own.

- 21,737
- 8
- 62
- 86