title: Get Managed Bean from servlet filter (is null)
body: <p>I've seen many posts like mine, but none could helped me. </p>
  
  <p>this is my managed bean and it's sessionScoped, if the login is ok it redirects to index page else displays error  </p>
  
  <pre><code>@ManagedBean
  @SessionScoped
  public class LoginBean implements Serializable  {
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  private static final String[] users = {"anna:qazwsx","kate:123456"};
  
  private String username;
  private String password;
  
  private boolean loggedIn     
  public String doLogin() {
      for (String user: users) {
          String dbUsername = user.split(":")[0];
          String dbPassword = user.split(":")[1];
  
          // Successful login
          if (dbUsername.equals(username) &amp;&amp; dbPassword.equals(password)) {
              loggedIn = true;
              return "/tmpl/home/index.xhtml?faces-redirect=true";
          }
      }
  
      // Set login ERROR
      FacesMessage msg = new FacesMessage("Login error!", "ERROR MSG");
      msg.setSeverity(FacesMessage.SEVERITY_ERROR);
      FacesContext.getCurrentInstance().addMessage(null, msg);
  
      return "/login/login.xhtml";
   }
  
   public boolean isLoggedIn() {
      return loggedIn;
   }
  }
  </code></pre>
  
  <p>The view, all is normal here , calls the doLogin method of managed bean </p>
  
  <pre><code>&lt;h:form id="login-form"&gt;
   &lt;h:messages /&gt;
   &lt;h:outputText value="Nom d'utilisateur:"/&gt;
   &lt;h:inputText value="#{loginBean.username}" id="username"/&gt;
   &lt;br/&gt;
   &lt;h:outputText value="Mot de passe:"/&gt;
   &lt;h:inputSecret value="#{loginBean.password}" id="password"/&gt;
   &lt;br/&gt;
   &lt;h:commandButton id="button" value="Login" action="#{loginBean.doLogin}" /&gt;    
   &lt;br/&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>The filter: if the user is authenticated then the loginBean is not null and it's logged </p>
  
  <pre><code>public class LoginFilter implements Filter {
  
  public void doFilter(ServletRequest request, ServletResponse response,   FilterChain chain) throws IOException, ServletException {      
      HttpSession session = ((HttpServletRequest) request).getSession(false);
      LoginBean loginBean = (session != null) ? (LoginBean) session.getAttribute("loginBean") : null;
  
  
      if (loginBean!=null)
          System.out.println(loginBean.getUsername());
  
      if (loginBean == null || !loginBean.isLoggedIn()) {
          System.out.println("here agai");
          String contextPath = ((HttpServletRequest)request).getContextPath();
          ((HttpServletResponse)response).sendRedirect(contextPath + "/login/login.xhtml");
      }         
      chain.doFilter(request, response);             
    }
  }
  </code></pre>
  
  <p>Why my managed bean (loginBean) is null?</p>