Home » Core Java » How to Configure Session timeout in Servlet

How to Configure Session timeout in Servlet

Session timeout in Servlet can be configured in two ways:
(1) In web.xml using session-config tag
(2) Using setMaxInactiveInterval() method of HttpSession

In web.xml using session-config tag

Using session-config , session timeout of the for user can be configured in web.xml. Time mentioned in session-config parameter is in minutes only.

<webapp....>
  <session-config>
     <session-timeout> 10 </session-timeout>
  </session-config>
</webapp>

Here, the session timeout is 10 minutes.

Using setMaxInactiveInterval() method of HttpSession

session timeout can also be configured using setMaxInactiveInterval() method of HttpSession. The time which is passed as an argument in setMaxInactiveInterval() is in seconds only.

HttpSession session = request.getSession(); // request object is of type ServletRequest
session.setMaxInactiveInterval(20*60);

Here, the time is in seconds i.e. session timeout is 20*60 seconds = 20 minutes.