title: Session timeout confusion - session.setMaxInactiveInterval(0)
body: <p>I am new to JEE and this is what confuses me. According to <a href="http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpSession.html#setMaxInactiveInterval%28int%29" rel="nofollow">HttpSession.html#setMaxInactiveInterval(int interval)</a> documentation </p>
  
  <blockquote>
    <p>An <code>interval</code> value of <code>zero</code> or less indicates that the <strong>session should never timeout</strong>.</p>
  </blockquote>
  
  <p>but according to my text book (which already is few years old - so I expect it not to be always right) using zero as argument should cause session to timeout immediately. </p>
  
  <p>This code </p>
  
  <pre><code>public class Test extends HttpServlet {
  
      protected void doGet(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
  
          response.setContentType("text/html");
          response.setCharacterEncoding("utf-8");
  
          PrintWriter out = response.getWriter();
  
          HttpSession session = request.getSession();
          session.setAttribute("foo", 42);
          session.setMaxInactiveInterval(0);
  
          out.println(session.getAttribute("foo"));//problem here
  
      }
  }
  </code></pre>
  
  <p>used on Glassfish 4.0 seems to confirm theory from textbook instead of newer official documentation because it returns <em>HTTP Status 500 - Internal Server Error</em> with error message </p>
  
  <blockquote>
    <p><code>java.lang.IllegalStateException: getAttribute: Session already invalidated</code></p>
  </blockquote>
  
  <p>What is going on here? Is this Glassfish 4.0 bug or documentation is wrong? Or maybe there is third option?</p>
  
  <hr>
  
  <p>PS. This code works as it should with negative values (session is not invalidated) and I am using <code>-1</code> instead of <code>0</code> in my code. I am just interested what is wrong with <code>0</code>.</p>