views:

80

answers:

2

Hi,

Is there a way to "catch" the session timeout event, so as to retrieve data from HttpSession before its invalidated ?

We're implementing the Filter Interface, and in the doFilter method, the user we stored in the session object at login is null when session times out.

Thanks in advance.

+7  A: 

You should be able to register an HttpSessionListener for your webapp that will allow you to get notified when a Session is destroyed.

It has two callback methods:

  • public void sessionCreated(HttpSessionEvent se)
  • public void sessionDestroyed(HttpSessionEvent se)

The HttpSessionEvent class has a getSession method that should let you get the affected session.

The listener class is registered in the web.xml

<listener>
  <description>My Session Listener</description>
  <listener-class>
    my.package.MySessionListener
  </listener-class>
</listener>
jimr
thanks, but will I be able to do a `session.getAttribute()` inside the sessionDestroyed method?
Tom
The HttpSessionEvent lets you get the HttpSession object, so you should be able to.
jimr
A: 

Have the user object implement HttpSessionBindingListener, then it can see for itself when it is added to or removed from the session.

EJP