title: How to effectively destroy 'session' in Java Servlet?
body: <p>The Servlet I'm working has a variable <code>session</code>.</p>
  
  <p>I've tried <code>session.invalidate();</code>, this seem to have destroyed session but when I do a redirect like so <code>response.sendRedirect("restanes.jsp");</code>, it gives me <code>HTTP Status 500</code> error with this line:</p>
  
  <pre><code>java.lang.IllegalStateException: getAttribute: Session already invalidated
  </code></pre>
  
  <p>This is expected since I was trying to destroy the session. </p>
  
  <p>But why is the page unable to redirect? On the same page elsewhere I've redirected successfully.</p>
  
  <p>How can I destroy session and redirect successfully? </p>
  
  <p>Code snippet:</p>
  
  <pre><code>if(request.getParameter("logout") != null ){  
          session.invalidate();
          response.sendRedirect("restanes.jsp");
  }
  </code></pre>
  
  <p>Update:
  All I needed to do was <code>return;</code> after <code>response.sendRedirect("restanes.jsp");</code>. Sincere thanks to <code>BalusC</code>.</p>