title: How to redirect to the login page when the session expires?
body: <p>I have three JSF 2.0 web modules and I need to redirect to the login page when the session expires. </p>
  
  <p>I have tried it using a <code>HttpSessionListener</code>, it is calling the <code>sessionDestroyed()</code> event method, but I am not able to forward/redirect the request in there. I think it is becasue there are no <code>HttpServletRequest</code> and <code>HttpServletResponse</code> objects.</p>
  
  <p>I also tried it using a <code>PhaseListener</code>, but it results in a "too many redirects" error in the webbrowser.</p>
  
  <pre><code>public class SessionListener implements PhaseListener {
  
      public PhaseId getPhaseId() {
          return PhaseId.RESTORE_VIEW;
      }
  
      public void beforePhase(PhaseEvent event) {
          if (!FacesContext.getCurrentInstance().isPostback()) {
              try {
                  System.out.println("Session Destroyed");
                  FacesContext.getCurrentInstance().getExternalContext().redirect("login.jsf");
              }
              catch (Exception e) {
                  System.out.println("error" + e);
              }
          }
      }
  
      public void afterPhase(PhaseEvent event)  {
          try {
              System.out.println("Session Created");
          }
          catch (Exception e) {
              System.out.println("error" + e);
          }
      }
  
  }
  </code></pre>
  
  <p>Why do those attempts not work and how can I solve it the best?</p>