title: Using Java sessions for login/logout
body: <p>Having trouble getting session to work in my Java application.</p>
  
  <p>My login.jsp page calls this <code>LoginAction</code> page.</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>package struts.action;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  
  import struts.form.LoginForm;
  
  public class LoginAction extends org.apache.struts.action.Action {
  private final static String SUCCESS = "success";
  private final static String FAILURE = "failure";
  
      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
          LoginForm lf = (LoginForm) form;
          HttpSession session = request.getSession(true);
          if (lf.getUsername().equals(lf.getPassword())) {
  
              session.setAttribute("Username", lf.getUsername());
              System.out.println(session.getAttribute("Username"));
              return mapping.findForward(SUCCESS);
  
          } else {
              return mapping.findForward(FAILURE);
          }
      }
  }
  </code></pre>
  
  <p>Corresponding LoginForm page</p>
  
  <pre class="lang-java prettyprint-override"><code>package struts.form;
  import org.apache.struts.action.*;
  
  public class LoginForm extends ActionForm{
  
      private String username;
      private String password;
  
      public LoginForm() {
          super();
      }
      private static final long serialVersionUID = 104092268304152302L;
  
      public String getUsername() {
      return username;
      }
  
      public void setUsername(String username) {
      this.username = username;   
  }
  
      public String getPassword() {
          return password;
          }
  
          public void setPassword(String password) {
          this.password = password;   
      }
  }
  </code></pre>
  
  <p>success.jsp, the page shown when logged in</p>
  
  <pre class="lang-java prettyprint-override"><code>&lt;%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %&gt;
  &lt;%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %&gt;
  &lt;%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %&gt;
  &lt;%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %&gt;
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;&lt;%@page
      language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;success&lt;/title&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;H1&gt;Hello: &lt;% session.getAttribute("Username"); %&gt;&lt;/H1&gt;
  
   &lt;html:form action="/LogoutAction" &gt;
          &lt;html:submit value="Logout" /&gt;
   &lt;/html:form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Logout Action page
      package struts.action;</p>
  
  <pre class="lang-java prettyprint-override"><code>import javax.servlet.http.HttpSession;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  
  public class LogoutAction extends org.apache.struts.action.Action {
  
      private final static String SUCCESS = "success";
      private final static String FAILURE = "failure";
  
      @Override
      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
      throws Exception {
          HttpSession session = request.getSession(true);
          System.out.println(session.getAttribute("Username"));
  
          try{
              session.removeAttribute("Username");
              session.invalidate();
              return mapping.findForward(SUCCESS);
          }catch(Exception ex){
              System.out.println("Error");
          }
      return mapping.findForward(FAILURE);
      }
  } 
  </code></pre>
  
  <p>Corresponding LogoutForm
      package struts.form;
      import org.apache.struts.action.*;</p>
  
  <pre class="lang-java prettyprint-override"><code>public class LogoutForm extends ActionForm{
  
      private static final long serialVersionUID = 1L;
  
  }
  </code></pre>
  
  <p>So the session is created in my Login action, and it works, as if I use the getAttribute() and print it to the console, the username comes up. However, the username won't show up on my success.jsp page.</p>
  
  <p>Can anyone help?</p>