Wednesday, February 4, 2009

Setting Session Timeout (J2EE)

There are two ways of setting session timeout in Java. One is by utilizing the deployment descriptor and the other is by calling setMaxInactiveInterval() under HttpSession interface.

Using Deployment Descriptor:



Setting time-out at the deployment descriptor gives impact to the whole web-application. Looking at the example above, the session for the whole web application will terminate in 5 minutes (note: In deployment descriptor the time used for the timeout is measured in minutes)


Using setMaxInactiveInternal() :

HttpSession session = request.getSession() ;
session.setMaxInactiveInterval( 5 * 60) ;

The difference between using setMaxInactiveInterval() in a program and using in deployment descriptor are:

  • setMaxInactiveInterval() method will only cause session time-out for a particular session instance and not the whole web-application.
  • Other than that in setMaxInactiveInterval() method, time is measured in seconds not minutes. Take a look at the code above, to reach 5 minutes expiry (5*60) is used.

Author: Thiagu