title: Stateful Bean : serlvet takes wrong value while managedBean not
body: <p>I've got a <code>Stateful Session Bean</code> which holds my login session, a <code>JSF Session Bean</code> and a <code>Servlet Filter</code>.</p>
  
  <p>What I ned to do is to stop non logged users from accessing my pages, so I did a filter.</p>
  
  <p>The <code>doFilter()</code> its like this :</p>
  
  <pre><code>public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
      HttpServletRequest req = (HttpServletRequest) request;
      String path = req.getRequestURI().substring(req.getContextPath().length());
      System.out.println(userManager.isLogged());
      if (userManager.isLogged() || path.equals("/") || path.equals("/index.xhtml") || path.startsWith(ResourceHandler.RESOURCE_IDENTIFIER) || path.startsWith("/resources/") || path.startsWith("/admin") || path.equals("/admin/login.xhtml")) {
          chain.doFilter(request, response);
      } else {
          request.getRequestDispatcher("/error.xhtml").forward(request, response);
      }
  }
  </code></pre>
  
  <p>where <code>userManager</code> is found with :</p>
  
  <pre><code>private UserManagerLocal lookupUserManagerLocal() {
      try {
          Context c = new InitialContext();
          return (UserManagerLocal) c.lookup("java:global/UNILIFE/UNILIFE-ejb/UserManager!ejb.UserManagerLocal");
      } catch (NamingException ne) {
          Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
          throw new RuntimeException(ne);
      }
  }
  </code></pre>
  
  <p>Now, <code>System.out.println(userManager.isLogged());</code> prints <strong>always</strong> false, while <code>#{loginBean.logged}</code> prints true.</p>
  
  <p>Note that <code>loginBean.logged</code> is just</p>
  
  <pre><code>public boolean isLogged() {
      return userManager.isLogged();
  }
  </code></pre>
  
  <p>and, in my Managed Bean, <code>userManager</code> is retrieved with </p>
  
  <pre><code>@EJB
  private UserManagerLocal userManager;
  </code></pre>
  
  <p>Seems like that the servlet does not take the same SFSB as the JSF Managed Bean.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>EDIT : new code</p>
  
  <p>servlet</p>
  
  <pre><code>UserManagerLocal userManager = lookupUserManagerLocal();
  private UserManagerLocal lookupUserManagerLocal() {
      try {
          Context c = new InitialContext();
          UserManagerLocal userM = (UserManagerLocal) c.lookup("java:global/UNILIFE/UNILIFE-ejb/UserManager!ejb.UserManagerLocal");
          HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
          req.setAttribute("userManager", userM);
          return userM;
      } catch (NamingException ne) {
          Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
          throw new RuntimeException(ne);
      }
  }
  </code></pre>
  
  <p>jsf bean</p>
  
  <pre><code>@PostConstruct
  public void init(){
      HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
      userManager = (UserManagerLocal) req.getSession().getAttribute("userManager");
  }
  </code></pre>