Reputation: 873
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();
Upvotes: 3
Views: 3195
Reputation: 1108932
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();
Upvotes: 3