title: How to use @SessionScoped with Guice
body: <p>Hi<br>
  I am currently playing with Guice and @SessionScoped. To give it more sense, I decided to build a (very simple) authentication process.  </p>
  
  <p>Below, I will explain each step I have done. Then I will ask you some questions.  </p>
  
  <p><strong>[1]</strong>  I have create an Identity class which represents a person (guest or user) :</p>
  
  <pre><code>@SessionScoped
  public class Identity implements Serializable
  {
      private String uid;
      private String name;
  
      public boolean isAuthenticate()
      {
          return uid != null;
      }
  
      public void logout()
      {
          this.uid = null;
      }
  
      /*Setters-Getters*/
  }
  </code></pre>
  
  <p><strong>[2]</strong>  Next, I created an Authentication class that log-in user:</p>
  
  <pre><code>public class Authentication
  {
      @Override
      public Identity authenticate(String login, String password)
      {
          /*some code*/
  
          Identity identity = new Identity();
          identity.setUid(user.getId());
          return identity;
      }
  }
  </code></pre>
  
  <p><strong>[3]</strong>  Then, in my Servlet, I log-in the user :</p>
  
  <pre><code>@RequestScoped
  public class LoginAction
  {
      @Inject
      Injector injector;
  
      protected void login(HttpServletRequest req, HttpServletResponse resp)
      {
              Identity identity = injector.getInstance(Identity.class);
              Authentication auth = new Authentication();
              identity = auth.authenticate("login","password");
      }
  }
  </code></pre>
  
  <p><strong>[4]</strong>  Finally, I create a Filter that show me if user is authenticated :</p>
  
  <pre><code>@Singleton
  public class SecurityFilter implements Filter
  {
      @Inject
      private Injector injector;
  
      @Override
      public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain)
      {
          Identity identity = injector.getInstance(Identity.class);
  
          if(identity.isAuthenticate())
          {
              System.err.println("USER");
          }
          else
          {
              System.err.println("GUEST");
          }
  
          chain.doFilter(request, response);
      }
  }
  </code></pre>
  
  <p>Well, this code is not working. My Identity's uid is always "null".</p>
  
  <p>Let's go for questions :  </p>
  
  <p><strong>a - First of all, Why did my code not works ?<br>
  b - Is @SessionScoped equivalent to set the object in HttpSession ?<br>
  c - How to invalidate the Identity object (only it) in (http)session ?<br>
  d - Generally, In which case did we have to use @SessionScoped?</strong></p>
  
  <p>Thanks you for reading,<br>
  Waiting your answers.</p>