title: JSF / primefaces Session - Session gets lost
body: <p>I´m new at JSF programming and got a problem with my login/session which gets lost after the login.</p>
  
  <p>I want to implement an easy login where a user can type in username and password. So I wrote a LoginController:</p>
  
  <pre><code>@ManagedBean
  @SessionScoped
  public class LoginController extends AbstractController{
  
       @PostConstruct
      public void initialiseSession() {
          FacesContext.getCurrentInstance().getExternalContext().getSession(true);
      }
  
      private String username = "null";
      private String password;
      private boolean loggedIn = false;
  
      @Inject 
      private EmployeeService employeeService;
  
  
      public static final String employeeSessionKey = "user";
  
      public LoginController() {
  
      }
  
      public String login() {
  //check username and password and if true redirect to "/"
  }
  </code></pre>
  
  <p>My login.xhtml looks like:</p>
  
  <pre><code>&lt;h:form id="loginForm"&gt;
  
              &lt;h:outputLabel style="font-size:24px" value="Bitte melden Sie sich an!"/&gt;
  
              &lt;p:panelGrid columns="2"&gt;
  
                  &lt;p:outputLabel id="userOutput"  for="userInput" value="Benutzername"/&gt;
                  &lt;p:inputText id="userInput" value="#{loginController.username}"&gt;&lt;/p:inputText&gt;
                  &lt;p:outputLabel id="passwordOutput" for="passwordInput" value="Passwort"/&gt;
                  &lt;p:inputText id="passwordInput" type="password" value="#{loginController.password}"&gt;&lt;/p:inputText&gt;
  
                  &lt;h:outputText value="Logindaten merken?" id="outputRememberLogin"&gt;
                      &lt;p:selectBooleanCheckbox id="loginCheckbox"&gt;
                      &lt;/p:selectBooleanCheckbox&gt;
                      &lt;p:spacer width="10" id="loginFormLittleSpacer"&gt;&lt;/p:spacer&gt;
                  &lt;/h:outputText&gt;
  
                  &lt;p:commandButton id="loginButton" value="Anmelden" action="#{loginController.login()}" ajax="false" &gt;
                  &lt;/p:commandButton&gt;
              &lt;/p:panelGrid&gt;
          &lt;/h:form&gt;  
  </code></pre>
  
  <p>So when I login the redirect works. But when I go to another .xhtml page the session gets lost. </p>
  
  <p>To test this, I put </p>
  
  <pre><code>&lt;p:outputLabel value="#{loginController.username}"/&gt;
  </code></pre>
  
  <p>on my pages. After the login, the username becomes "null". </p>
  
  <p>I´m going crazy on this problem.</p>
  
  <p>Any ideas?</p>
  
  <p>thanks before. </p>