Now they tell me that this will end the session (or is it all sessions?) At the 15th minute of use, regardless of their activity.
This is not true . It will simply kill the session when the associated client (web browser) does not gain access to the website for more than 15 minutes. Activity, of course, expects, as expected, to see your attempt to solve this problem.
HttpSession#setMaxInactiveInterval()
, by the way, doesn't change much. It is exactly the same as <session-timeout>
in web.xml
, with the only difference being that you can change / set it programmatically at runtime. By the way, the change only affects the current instance of the session, and not globally (otherwise it would be a static
method).
To play and experience this yourself, try setting <session-timeout>
to 1 minute and create an HttpSessionListener
as follows:
@WebListener public class HttpSessionChecker implements HttpSessionListener { public void sessionCreated(HttpSessionEvent event) { System.out.printf("Session ID %s created at %s%n", event.getSession().getId(), new Date()); } public void sessionDestroyed(HttpSessionEvent event) { System.out.printf("Session ID %s destroyed at %s%n", event.getSession().getId(), new Date()); } }
(if you are not already on Servlet 3.0 and therefore cannot use @WebListener
, then register in web.xml
as follows):
<listener> <listener-class>com.example.HttpSessionChecker</listener-class> </listener>
Note that the servletcontainer will not immediately kill sessions after the exact timeout value. This is a background job that runs at regular intervals (for example, 5-15 minutes depending on the load and make / type of the servlet container). Therefore, do not be surprised when you do not see the destroyed
line on the console immediately after exactly one minute of inactivity. However, when you start an HTTP request for a session with a timeout but not destroyed, it will be destroyed immediately.
See also:
- How do servlets work? Create, Sessions, Shared Variables, and Multithreading
BalusC Jun 25 2018-10-15T00: 00Z
source share