title: Tomcat Session Timeout web.xml
body: <p>This is my web.xml file, it is located in <code>WEB-INF/lib</code>. It specifies session timeout at 1 minute, however it does not time the user out after 1 minute of activity.</p>
  
  <p><strong><code>Web.xml</code>:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;Login&lt;/servlet-name&gt;
          &lt;servlet-class&gt;Login&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Login&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/Login&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;session-config&gt;
          &lt;session-timeout&gt;1&lt;/session-timeout&gt;
      &lt;/session-config&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>I used this line <code>session.getMaxInactiveInterval()</code> in my jsp file and it outputted <code>1800</code> (30 minutes) . Does anyone why it defaults to 30 rather than using the time specified in my web.xml file?</p>
  
  <p>EDIT:</p>
  
  <p>I've code on my jsp page which checks for session attribute and if it does exist redirects the user to the login page after a minute even on page refresh the user is not redirected.</p>
  
  <pre><code>if(session.getAttribute("username") != null){
                              out.println(session.getAttribute("username"));
                          }else{
                              response.setStatus(response.SC_MOVED_TEMPORARILY);
                              response.setHeader("Location", "index.jsp");
                          }
  </code></pre>
  
  <p>EDIT Full Code (Login.java):</p>
  
  <pre><code>package com.labs.xmlgenerator.controller.managesession;
  
  import java.io.IOException;
  import java.sql.SQLException;
  import java.util.HashMap;
  
  import javax.servlet.ServletContext;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.Cookie;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  import com.labs.xmlgenerator.model.dbconnection.*;
  /**
   * Servlet implementation class Login
   */
  @WebServlet(description = "Verifies Users Credentials", urlPatterns = { "/Login" })
  public class Login extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      private DbLoginQueries query = new DbLoginQueries();
  
  
      /**
       * @see HttpServlet#HttpServlet()
       */
      /*
      public Login() {
          super();
          // TODO Auto-generated constructor stub
      }*/
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
          try {
              validateLoginCredentials(request.getParameter("liUsr"),request.getParameter("liPwd"),request,response);
          } catch (ClassNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  
      private void validateLoginCredentials (String username, String password, HttpServletRequest request, HttpServletResponse response) throws IOException, ClassNotFoundException, SQLException{
  
          Boolean valid = true;
          int resultSize;
          HashMap&lt;String, String&gt; userDetails = null;
          HttpSession session = request.getSession(true);
          String location = "index.jsp";
          Cookie usernameErrorCookie;
          Cookie passwordErrorCookie;
          Cookie usernameCookie;
  
          if(username == null || username == ""){
              valid = false;
              usernameErrorCookie = new Cookie("liUsrErrCookie","Please enter a valid username");
              response.addCookie(usernameErrorCookie);
          }else{
              usernameCookie = new Cookie("liUsrCookie",username);
              response.addCookie(usernameCookie);
          }
  
          if(password == null || password == ""){
              valid = false;
              passwordErrorCookie = new Cookie("liPwdErrCookie","Please enter a valid password");
              response.addCookie(passwordErrorCookie);
          }
  
          if(valid == true){
              userDetails = query.loginQuery(username);
              resultSize = userDetails.size();
              if(resultSize &lt; 4){
                  valid = false;
                  usernameErrorCookie = new Cookie("liUsrErrCookie","The username entered is not valid");
                  response.addCookie(usernameErrorCookie);
              }
              else if(resultSize &gt; 4){
                  valid = false;
                  usernameErrorCookie = new Cookie("liUsrErrCookie","The username is returning more than one result, please contact admin");
                  response.addCookie(usernameErrorCookie);
              }
              else if(resultSize == 4){
  
                  if(!userDetails.get("Password").equals(password)){
                      valid = false;
                      passwordErrorCookie = new Cookie("liPwdErrCookie","The entered password is incorrect");
                      response.addCookie(passwordErrorCookie);
                  }
              }
          }
  
  
          if(valid == true){
              session.setAttribute("username", userDetails.get("Username"));
              session.setAttribute("permission", userDetails.get("AdminPermissions"));
              session.setAttribute("email", userDetails.get("Email"));
              location = "home.jsp";
  
          }else{
              location = "index.jsp#login";
          }
  
          response.setStatus(response.SC_MOVED_TEMPORARILY);
          response.setHeader("Location", location);
  
      }
  }
  </code></pre>
  
  <p>home.jsp</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;
  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /&gt;
  &lt;title&gt;Home&lt;/title&gt;
      &lt;link rel="stylesheet" href="resources/css/Common.css" type="text/css"&gt;
      &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script src="resources/js/Common.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;header&gt;
          &lt;div id="actionsMenu"&gt;
              &lt;ul id="actionLinks"&gt;
                  &lt;li&gt;&lt;a class="linkButtons" id="userNameLink"&gt;
                                       &lt;%
                          System.out.println(session.getMaxInactiveInterval());
                          if(session.getAttribute("username") != null){
                              out.println(session.getAttribute("username"));
                          }else{
                              out.println("no user");
                              /*
                              response.setStatus(response.SC_MOVED_TEMPORARILY);
                              response.setHeader("Location", "index.jsp");*/
                          }   
                      %&gt;
                  &lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a class="linkButtons" href="Logout"&gt;Log Out&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a class="linkButtons"  href="#"&gt;Update&lt;/a&gt;&lt;/li&gt;
              &lt;/ul&gt;
          &lt;/div&gt;
      &lt;/header&gt;
      &lt;nav class="tabs"&gt;
          &lt;section id="generateSection"&gt;
              &lt;h2 class="selectedTab" id="generateTab"&gt;
                  &lt;a id="gene" href="#generateXML"&gt;Generate XML&lt;/a&gt;
              &lt;/h2&gt;
              &lt;div class="selectedContent" id="generateNav"&gt;
                  &lt;ul id="links"&gt;
                      &lt;li&gt;&lt;a class="navLink" href="#graphic"&gt;Graphic Interface&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;a class="navLink" href="#xml"&gt;XML Interface&lt;/a&gt;&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/div&gt;
          &lt;/section&gt;
          &lt;section id="adminSection"&gt;
              &lt;h2 class="normalTab" id="adminTab"&gt;
                  &lt;a href="#admin"&gt;Admin&lt;/a&gt;
              &lt;/h2&gt;
              &lt;div class="normalContent" id="adminNav"&gt;
                  &lt;ul id="links"&gt;
                      &lt;li&gt;&lt;a class="navLink" href="#images"&gt;Manage Images&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;a class="navLink" href="#keywords"&gt;Manage Keywords&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;a class="navLink" href="#users"&gt;Manage Users&lt;/a&gt;&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/div&gt;
          &lt;/section&gt;
      &lt;/nav&gt;  
      &lt;div id="content"&gt;
          &lt;noscript&gt;&lt;p id="javascriptError"&gt;This website requires JavaScript to be enabled.&lt;/p&gt;&lt;/noscript&gt;
      &lt;/div&gt;
      &lt;div id="updateUserDetails"&gt;&lt;/div&gt;
      &lt;div id="popup"&gt;
          &lt;input type="button" value="X" id="exitButton"&gt;
          &lt;p class="pageTitle" style="float:left; margin:0px;"&gt;Update Details&lt;/p&gt;
          &lt;form id="updateForm"  action="Update" onsubmit="return updateValidation()" method="post" &gt;
              &lt;p id="user"&gt;Username :&lt;/p&gt;
              &lt;p id="userNameUpdate"&gt;&lt;%
                  if(session.getAttribute("username") != null){
                      out.println(session.getAttribute("username"));
                  }%&gt;&lt;/p&gt;
              &lt;p class="error" id="updCurrentPwdErr"&gt;
              &lt;% 
                          Cookie[] currentPassEror = null;
                          currentPassEror = request.getCookies();
                          if(currentPassEror != null){
                              for(int i = 0; i &lt; currentPassEror.length; i++){
                                   Cookie cookie = currentPassEror[i];
                                   if(cookie.getName().equals("updCurrentPwdErrCookie")){
                                       out.println(cookie.getValue());
                                       cookie.setMaxAge(0);
                                       response.addCookie(cookie);
                                   }
                               }
                          }
                          %&gt;
              &lt;/p&gt;
              &lt;label for="updCurrentPwdLbl"&gt;Current Password :&lt;/label&gt;
              &lt;br /&gt;
              &lt;input type="password" name="updCurrentPwd" id="updCurrentPwd"&gt;
              &lt;br /&gt;
              &lt;p class="error" id="updNewPwdErr"&gt;
              &lt;% 
                          Cookie[] newPassCookies = null;
                          newPassCookies = request.getCookies();
                          if(newPassCookies != null){
                              for(int i = 0; i &lt; newPassCookies.length; i++){
                                   Cookie cookie = newPassCookies[i];
                                   if(cookie.getName().equals("updNewPwdErrCookie")){
                                       out.println(cookie.getValue());
                                       cookie.setMaxAge(0);
                                       response.addCookie(cookie);
                                   }
                               }
                          }
              %&gt;&lt;/p&gt;
              &lt;label for="updNewPwdLbl"&gt;New Password :&lt;/label&gt;
              &lt;br /&gt;
              &lt;input type="password" id="updNewPwd" name="updNewPwd"&gt;
              &lt;br /&gt;
              &lt;p class="error" id="updReNewPwdErr"&gt;
              &lt;% 
                          Cookie[] reNewPassCookies = null;
                          reNewPassCookies = request.getCookies();
                          if(reNewPassCookies != null){
                              for(int i = 0; i &lt; reNewPassCookies.length; i++){
                                   Cookie cookie = reNewPassCookies[i];
                                   if(cookie.getName().equals("updReNewPwdErrCookie")){
                                       out.println(cookie.getValue());
                                       cookie.setMaxAge(0);
                                       response.addCookie(cookie);
                                   }
                               }
                          }
              %&gt;          
              &lt;/p&gt;
              &lt;label for="updReNewPwdLbl"&gt;Re-Enter New Password :&lt;/label&gt;
              &lt;br /&gt;
              &lt;input type="password" id="updReNewPwd" name="updReNewPwd"&gt;
              &lt;br /&gt;
              &lt;p class="error" id="updEmailErr"&gt;
              &lt;% 
                          Cookie[] emailErrCookies = null;
              emailErrCookies = request.getCookies();
                          if(emailErrCookies != null){
                              for(int i = 0; i &lt; emailErrCookies.length; i++){
                                   Cookie cookie = emailErrCookies[i];
                                   if(cookie.getName().equals("updEmailErrCookie")){
                                       out.println(cookie.getValue());
                                       cookie.setMaxAge(0);
                                       response.addCookie(cookie);
                                   }
                               }
                          }
              %&gt;
              &lt;/p&gt;
              &lt;label for="updEmailLbl"&gt;Email :&lt;/label&gt;
              &lt;br /&gt;
              &lt;input type="text" id="updEmail" name="updEmail" value="&lt;%
                      boolean foundEmailCookie = false;
                      Cookie[] emailCookies = null;
              emailCookies = request.getCookies();
                      if(emailCookies != null){
                          for(int i = 0; i &lt; emailCookies.length; i++){
                               Cookie cookie = emailCookies[i];
                               if(cookie.getName().equals("updEmailCookie")){
                                   foundEmailCookie = true;
                                   out.println(cookie.getValue());
                                   cookie.setMaxAge(0);
                                   response.addCookie(cookie);
                               }
                           }
                      }
                      if(!foundEmailCookie){
                          if(session.getAttribute("email") != null){
                              out.println(session.getAttribute("email"));
                          }
                      }
              %&gt;"&gt;
              &lt;input type="hidden" id="updUrl" name="updUrl" value=""&gt;
              &lt;br /&gt;
              &lt;input type="submit" value="UPDATE"&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>