title: Why is Phase Listener-based JSF redirect not working for AJAX requests when session has timed-out?
body: <p>I have a JSF Phase Listerner that checks to see if the user is logged in, and if not, redirects them to the login page. This is working fine for non-ajax requests. However, if the user is on a page, in my case, one that has a primefaces data table, and clicks on a button that invokes an ajax request -- but their session has timed out -- the code gets executed that issues the redirect (using <code>ExternalContext#redirect</code>), however the user is not navigated to the login page. </p>
  
  <p>Any idea why this is not working?</p>
  
  <p>Here is my phase listener:</p>
  
  <pre><code>private static final String IS_LOGGED_IN_INDICATOR = "loggedIn";
  private static final String LOGIN_PAGE = "/login.jsp";
  
  public PhaseId getPhaseId() {
      return PhaseId.RESTORE_VIEW;
  }
  
  public void beforePhase(PhaseEvent event) {
      ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
      HttpSession session = (HttpSession)ec.getSession(false);
  
      if (session==null || session.getAttribute(IS_LOGGED_IN_INDICATOR) == null) {
         try {
              ec.redirect(LOGIN_PAGE);
         }
         catch(IOException e) {
             // log exception...
         }            
      }
  }
  
  public void afterPhase(PhaseEvent event) {
      // no-op
  }   
  </code></pre>
  
  <p>}</p>