O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.1. Setting the Session Timeout in web.xml

Problem

You want to configure a timeout period for the web application in the deployment descriptor.

Solution

Create a session-config element in web.xml.

Discussion

The length of time that a session lasts before the server invalidates the session and unbinds any of its objects is an important component of your web application. In Tomcat 4.1.x, the default timeout period for a session is 30 minutes. If any requests that are associated with the session have been inactive for that period, the session times out. If the user decides to return to the web application after 30 minutes, using the same browser, then a new session is created for him. Example 11-1 shows how to set your own timeout period for sessions.

Example 11-1. Configuring the session timeout

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
           "http://java.sun.com/dtd/web-application_2_3.dtd"
>

<web-app>

<!-- filter, listener, servlet, and servlet-mapping elements precede session-config -->

  <session-config>
    <session-timeout>15</session-timeout>
  </session-config>

</web-app>

Place one nested session-timeout element within the session-config. The timeout is expressed as minutes, and overrides the default timeout (which is 30 minutes in Tomcat, for example). However, the HttpSession.getMaxInactiveInterval( ) method in a servlet returns the timeout period for that session in seconds; if your session is configured ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required