Search code examples
javahttpsession

Get expiring from HttpSession object


I'm trying to catch the sessionDestroyed event from a HttpSessionListener in order to check if was triggered by a sessionTimeout.

While I'm debugging I can see an attribute named "expiring" that according to the documentation this attribute is used to internally skip some Exceptions.

expiring We are currently processing a session expiration, so bypass certain IllegalStateException tests. Source

The problem is , since this attribute is set to "protected" I'm not able to check if this event was effectively triggered by a session timeout.

I need to catch this session timeout event to save a record on database.

Can someone help me?

This is what I have so far:

public class AppHttpSessionListener implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent se) {
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        LOG.log(Level.INFO, "DEBUG: SESION DESTRUIDA");
    }
    private static final Logger LOG = Logger.getLogger(AppHttpSessionListener.class.getName());

}

Solution

  • Maybe you can try to estimate it?

    @Override
        public void sessionDestroyed(HttpSessionEvent se) {
    
        HttpSession httpSession = se.getSession();
    
        long lastAccessedTime = httpSession.getLastAccessedTime();
        int maxInactiveTime = httpSession.getMaxInactiveInterval();
    
         if ((System.currentTimeMillis() - lastAccessedTime) >= (maxInactiveTime*1000) {
             LOG.log(Level.INFO, "DEBUG: SESION DESTRUIDA");
       }