Programmatically Determine Java Session Timeout

HttpSession.getMaxInactiveInterval provides this value

int getMaxInactiveInterval()

Returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses. After this interval, the servlet container will invalidate the session. The maximum time interval can be set with the setMaxInactiveInterval method.

A return value of zero or less indicates that the session will never timeout.

Returns: an integer specifying the number of seconds this session remains open between client requests


The session's timeout is determined by idle time so there is no way to know when it will timeout.

However, you can calculate the next possible timeout assuming session is not being accessed,

Date expiry = new Date(session.getLastAccessedTime() + session.getMaxInactiveInterval()*1000);

 


This is the code to get the time out value:

<%= session.getMaxInactiveInterval() %>

In a Servlet use:

int timeoutInSeconds = request.getSession().getMaxInactiveInterval();

In a JSP use:

<p>Timeout in seconds: ${pageContext.session.maxInactiveInterval}</p>

Tags:

Java

Servlets

Jsp