tags:

views:

507

answers:

2

I'm trying to adjust the session timeout using HttpSession.setMaxInactiveInterval and it's not working.

Here is my code (Groovy), which is executing without exceptions:

def paramValue = WebAttributes.REQUEST.getParameter('maxInactiveSeconds');
println 'paramValue=' + paramValue;
if (paramValue != null) {
  def seconds = Integer.parseInt(paramValue);
  WebAttributes.REQUEST.getSession().setMaxInactiveInterval(seconds);
}

Some details:

  • Tomcat 6.0.16
  • This is happening in a webapp separate from the 'normal' one (i.e. with visual content), but I have defined emptySessionPath="true" so the session *should* be shared across webapps

thanks,

haruspex

+1  A: 

How did you test it? Every new request would postpone the timeout again, do you know? So F5'ing the request until the expected timeout ain't going to help. The webcontainer will also not immediately destroy the session, so you won't see immediate result from any HttpSessionListener. It will reap them at certain intervals, which may be each minute, but can also be each 15 minutes. It will however reap it immediately if a new request came in after the timeout.

With regard to your "the session should be shared" phrase, best way to verify this is of course by checking the session ID, either programmatically (e.g. displaying ${pageContext.session.id}) or by just determining the value of the jsessionid cookie in your webbrowser.

To monitor the actual session creation and destroy, implement a dummy HttpSessionListener. Here's a basic example:

public class MyHttpSessionListener implements HttpSessionListener {
    public void sessionCreated(HttpSessionEvent event) {
        System.out.printf("%s session %s created %n", new Date(), event.getSession().getId());
    }
    public void sessionDestroyed(HttpSessionEvent event) {
        System.out.printf("%s session %s destroyed %n", new Date(), event.getSession().getId());
    }
}

Hope this helps.

BalusC
Thanks for your thoughtful and knowledgeable reply.I have a JavaScript-based session timeout warning message embedded on the page. It starts a timer with duration based on HttpSession.getMaxInactiveInterval(). My code doesn't appear to be altering this value at all -- I attempt to set it to 40 sec. (for automated testing) but it's still 30 min. when the page renters.
Drew Wills
A: 

Turns out there was another technology setting the maxInactiveInterval back to 30 min, overriding my attempts to change it.

Tomcat, Java, etc. were all working as expected.

Drew Wills