3

Is there a way to reset the timeout counter of a HTTPSession other than through serving a HTTP request?

I'm looking for something like

session.resetTimeout();
Twilite
  • 873
  • 9
  • 22

1 Answers1

3

So, you've a HttpSession at hands without having a concrete HttpServletRequest?

You could increment the max inactive interval with the sum of the current inactive time and the max inactive interval.

int maxInactiveInterval = session.getMaxInactiveInterval();
int currentInactiveTime = (int) (System.currentTimeMillis() - session.getLastAccessedTime() / 1000);
session.setMaxInactiveInterval(maxInactiveInterval + currentInactiveTime);

However, this requires some filter which resets it back to the default max inactive interval again on every request when its value is off from the default.

session.setMaxInactiveInterval(defaultMaxInactiveInterval);

On Servlet 3.0, this value is obtainable by SessionCookieConfig#getMaxAge().

int defaultMaxInactiveInterval = getServletContext().getSessionCookieConfig().getMaxAge();
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555