title: JSF login filter, session is null
body: <p>I've been trying to follow <a href="https://stackoverflow.com/questions/8480100/how-implement-a-login-filter-in-jsf">this</a> answer primarily but I always get redirected to my login.xhtml (except for when i log in from the login page) because this...</p>
  
  <pre><code>AppManager am = (AppManager) req.getSession().getAttribute("appManager");
  </code></pre>
  
  <p>Is always null.
  I've been trying to print out user info on the login screen and no matter how i get there all fields(username, password, loggedIn...) are always null, even if i type the adress straight from the admin page (that's where you get when you log in).
  How do I make it so that the session is saved, not whiped everytime i type in the adress manually/leave the page?</p>
  
  <p>AppManager:</p>
  
  <pre><code>import java.io.Serializable;
  import javax.ejb.EJB;
  import javax.enterprise.context.SessionScoped;
  import javax.faces.bean.ManagedBean;
  import javax.faces.context.FacesContext;
  import jobapp.controller.Controller;
  
  @ManagedBean(name="appManager")
  @SessionScoped
  public class AppManager implements Serializable {
  private static final long serialVersionUID = 16247164405L;
      @EJB
      private Controller controller;
      private String username;
      private String password;
      private boolean loggedIn;
      private Exception failure;
      ...
       /**
       * 
       * @param e an exception to handle.
       */
      private void handleException(Exception e) {
          e.printStackTrace(System.err);
          failure = e;
          FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
      }
  
      /**
       * The login method.
       * calls the controllers login method.
       * 
       */ 
      public void login(){
          try{
              failure = null; 
              loggedIn = controller.login(username, password);
  
          }catch (Exception e){
              handleException(e);
          }
      }
      /**
       * The logout method.
       * Sets the user's info to null
       * and stops the conversation.
       */
      public void logout(){
          username = null;
          password = null;
          loggedIn = false;
          FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
      }
  ...
  </code></pre>
  
  <p>Filter:</p>
  
  <pre><code>@WebFilter("/faces/admin.xhtml")
  public class LoginFilter implements Filter {
  ...
      @Override
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {    
          HttpServletRequest req = (HttpServletRequest) request;
          //TODO fix "am" nullpointer
          AppManager am = (AppManager) req.getSession().getAttribute("appManager");
          if (am != null &amp;&amp; am.isLoggedIn()) {
              // User is logged in, so just continue request.
              chain.doFilter(request, response);
          } else {
              // User is not logged in, so redirect to login.
              HttpServletResponse res = (HttpServletResponse) response;
              res.sendRedirect(req.getContextPath() + "/faces/login.xhtml");
          }
      }
  </code></pre>