title: JSF Login with HttpServletRequest
body: <p>i found a solution in stackoverflow how to code a login in JSF using HttpServletRequest.
  First things first, the login.xhtml:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"&gt;
  
  &lt;h:head&gt;
      &lt;title&gt;Login&lt;/title&gt;
  &lt;/h:head&gt;
  &lt;h:body&gt;
      &lt;h3&gt;Login here&lt;/h3&gt;
      &lt;h:form id="loginForm"&gt;
          &lt;h:outputLabel for="username" value="Username:" /&gt;
          &lt;h:inputText value="#{loginService.userName}" id="username" requried="true" /&gt;
          &lt;br/&gt;
          &lt;h:outputLabel for="password" value="Password:" /&gt;
          &lt;h:inputSecret value="#{loginService.password}" id="password" requried="true" /&gt;
          &lt;br/&gt;
          &lt;h:commandButton id="button" value="Login" action="#{loginService.doLogin}" /&gt;
          &lt;br/&gt;
          &lt;h:commandLink action="#{navigationService.redirectToIndex}" value="Home" /&gt;
          &lt;br/&gt;
          &lt;h:messages /&gt;
          &lt;br/&gt;
      &lt;/h:form&gt;
  &lt;/h:body&gt;
  </code></pre>
  
  <p></p>
  
  <p>The loginService:</p>
  
  <pre><code>@Named
  @SessionScoped
  public class LoginService implements Serializable {
  
  private String userName = "";
  private String password = "";
  @Inject
  private NavigationService navigationService = null;
  @Inject
  private String originalURL = "";
  
  /**
   * 
   */
  @PostConstruct
  public void init() {
      ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
  
      this.originalURL = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);
  
      if(this.originalURL == null) {
          this.originalURL = externalContext.getRequestContextPath() + navigationService.toIndex();
      } else {
          String originalQuery = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_QUERY_STRING);
  
          if(originalQuery != null) {
              this.originalURL += "?" + originalQuery;
          }
      }
  }
  
  /**
   * 
   * @return
   * @throws IOException 
   */
  public void doLogin() throws IOException {
      FacesContext context = FacesContext.getCurrentInstance();
      ExternalContext externalContext = context.getExternalContext();
      HttpServletRequest request = (HttpServletRequest)externalContext.getRequest();
  
      try {
          request.login(this.userName, this.password);
  
          User user = dao.findUserByUserName(userName);
  
          externalContext.getSessionMap().put("user", user);
          externalContext.redirect(this.originalURL);
      } catch(ServletException e) {
          context.addMessage(null, new FacesMessage("Unknown login"));
      } catch (NoSuchUserException e) {
          context.addMessage(null, new FacesMessage(e.getMessage()));
      }
  }
  
  /**
   * 
   * @return
   * @throws IOException 
   */
  public void doLogout() throws IOException {
      ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
  
      externalContext.invalidateSession();
      externalContext.redirect(externalContext.getRequestContextPath() + navigationService.toLogin());
  }
  
  // Getters and Setters
  }
  </code></pre>
  
  <p>The only thing i still need to know is now:</p>
  
  <p>Where can i define for which pages login is needed?</p>