Setting Session Timeout in JSP Servlet

Author posted by Jitendra on Posted on under category Categories JSP, Servlet and tagged as Tags with Leave a comment on Setting Session Timeout in JSP Servlet

How to set the Session Time out in JSP and Servlet

Session can die in following three ways in J2EE application:

  1. Its Time out
  2. invalidate() method called on Session Object
  3. Application goes down (Application crashes or undeployed)

Method 1: Configuring session timeout in the DD

Session Time out can be configured in DD (Deployment Descriptor / web.xml) and it has virtually same effect as calling setMaxInactiveInterval() on every session that is created :

<web-app>
...
<servlet>
....
</servlet>
<session-config>
    <session-timeout>15<session-timeout>
</session-config>
</web-app>

Note : Argument in tag is in minutes. In above example its 15 minutes.

Method 2:  Setting Session timeout for the specific session object

If you want to change the session timeout for any particular session then we can call below method on that Session :

session.setMaxInactiveInterval(30*60);

Note : the argument passed in method setMaxInactiveInterval() is in seconds. In Above example its 30 minutes.

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *