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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With