title: How to logout session using invalidate() in servlet
body: <pre><code>protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
          HttpSession session = request.getSession();
          if(session != null)
          {
              try
              {
                  response.setHeader("Cache-Control","no-cache"); //Forces caches to obtain a new copy of the page from the origin server
                  response.setHeader("Cache-Control","no-store"); //Directs caches not to store the page under any circumstance
                  response.setDateHeader("Expires", 0); //Causes the proxy cache to see the page as "stale"
                  response.setHeader("Pragma","no-cache"); //HTTP 1.0 backward compatibility
                  session.setAttribute("admin_name",null);
                  session.invalidate();
                  response.sendRedirect("login.jsp");
              }
              catch(Exception e)
              {
                  System.out.println(e.getMessage());
                  System.out.println(e);
              }
  
          }
          else
          {
  
  
          }
      }
  </code></pre>
  
  <p>This is my servlet. I am going to logout the session that i created using name admin_name. when i logout the session it successfully hit the login page. but when i press back button it go to the previous page. I didn't understood why these happen even if i invalidate that session. but when i refresh that page it will again hit the login page.</p>
  
  <p>Here the code that i used in jsp page.</p>
  
  <pre><code>&lt;%
          String name = null;
          if (session.getAttribute("admin_name") == null) {
              response.sendRedirect("login.jsp");
          } else {
              name = (String) session.getAttribute("admin_name");
          }
      %&gt;
  </code></pre>