title: JSF 2.0 : How to redirect to the protected page after using HttpServletRequest.login
body: <p>I'm trying to use HttpServletRequest.login with form based authentication.</p>
  
  <p>All is ok (the container tells if login/password are good), except that after the user has entered his login, I don't know how to redirect the user to the protected page he asked for (the login form is redisplayed). How to do that?</p>
  
  <p>Thanks in advance for your help.</p>
  
  <p>The code:</p>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;login-config&gt;
      &lt;auth-method&gt;FORM&lt;/auth-method&gt;
      &lt;realm-name&gt;security&lt;/realm-name&gt;
      &lt;form-login-config&gt;
          &lt;form-login-page&gt;/faces/loginwithlogin.xhtml&lt;/form-login-page&gt;
          &lt;form-error-page&gt;/faces/noaut.xhtml&lt;/form-error-page&gt;
      &lt;/form-login-config&gt;
  &lt;/login-config&gt;
  </code></pre>
  
  <p>Page loginwithlogin.xhtml</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"&gt;
      &lt;h:head&gt;
          &lt;title&gt;Authentication&lt;/title&gt;
      &lt;/h:head&gt;
      &lt;h:body&gt;
          &lt;h:form&gt;
              Login :
              &lt;h:inputText value="#{login.login}" required="true" /&gt;
              &lt;p/&gt;
              Mot de passe :
              &lt;h:inputSecret value="#{login.password}" required="true" /&gt;
              &lt;p/&gt;
              &lt;h:commandButton value="Connexion" action="#{login.submit}"&gt;
                   &lt;f:ajax execute="@form" render="@form" /&gt;
              &lt;/h:commandButton&gt;
              &lt;h:messages /&gt;
          &lt;/h:form&gt;
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Update: without Ajax it does not work.</p>
  
  <p>Backing bean:</p>
  
  <pre><code>@Named
  @SessionScoped
  public class Login implements Serializable {
    private String login;
    private String password;
    // getters and setters 
    ...
  
    public void submit() {
      FacesContext context = FacesContext.getCurrentInstance();
      HttpServletRequest request = 
              (HttpServletRequest) context.getExternalContext().getRequest();
      try {
          request.login(login, mdp);
          context.addMessage(null, 
                  new FacesMessage(FacesMessage.SEVERITY_INFO, 
                  "OK", null));
      } catch (ServletException e) {
          context.addMessage(null, 
                  new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                  "Bad login", null));
      }
    }
  
  }
  </code></pre>