title: JSF- can't log out
body: <p>Here is my the template I use in <code>index.xhtml</code>:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"&gt;
  &lt;h:head&gt;
      &lt;title&gt;&lt;ui:insert name="title"&gt;Default title&lt;/ui:insert&gt;&lt;/title&gt;
      &lt;h:outputStylesheet library="default" name="css/style.css" /&gt;
  &lt;/h:head&gt;
  &lt;h:body&gt;
      &lt;div id="wrapper" style="width: 800px; align: center"&gt;
          &lt;div id="header"&gt;
              &lt;div style="float: left"&gt;
                  &lt;h:graphicImage library="default"
                      name="img/032-ivan-the-terrible-theredlist.jpg" /&gt;
              &lt;/div&gt;
              &lt;h:form id="loginForm" rendered="#{sessionScope.user == null}"&gt;
                      Username: &lt;h:inputText id="username"
                      value="#{userController.user.username}" /&gt;
                      Password: &lt;h:inputSecret id="password"
                      value="#{userController.user.password}" /&gt;
                &lt;h:commandButton value="Login" action="#{userController.login}" /&gt;
              &lt;/h:form&gt;
              &lt;h:commandButton value="Logout" action="#{userController.logout}"
                  rendered="#{sessionScope.user != null}" /&gt;
              &lt;h:outputLink target="register.xhtml"
                  rendered="#{sessionScope.user == null}"&gt;Register&lt;/h:outputLink&gt;
              &lt;h:outputText rendered="#{sessionScope.user != null}"
                  value="You are logged in as #{sessionScope.user.username}" /&gt;
          &lt;/div&gt;
          &lt;div id="menu" style="clear: both"&gt;Menu&lt;/div&gt;
          &lt;div id="content"&gt;
              &lt;ui:insert name="content"&gt;Default content&lt;/ui:insert&gt;
          &lt;/div&gt;
          &lt;div id="footer"&gt;Footer&lt;/div&gt;
      &lt;/div&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here is the user controller. Register and log in work fine - but log out won't work for the life of me.</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class UserController {
  
      // http://stackoverflow.com/a/10691832/281545
      private User user;
      @EJB // do not inject stateful beans !
      private UserService service;
  
      public User getUser() {
          return user;
      }
  
      @PostConstruct
      void init() {
          // http://stackoverflow.com/questions/3406555/why-use-postconstruct
          user = new User();
      }
  
      public String login() {
          FacesContext context = FacesContext.getCurrentInstance();
          try {
              System.out.println("Pass :" + user.getPassword());
              user = service.find(user.getUsername(), user.getPassword());
              context.getExternalContext().getSessionMap().put("user", user);
              return "/index.xhtml?faces-redirect=true";
          } catch (NoResultException e) {
              context.addMessage(null, new FacesMessage(
                  FacesMessage.SEVERITY_ERROR, "Unknown login, please try again",
                  null));
              return null;
          }
      }
  
      public String register() {
          FacesContext context = FacesContext.getCurrentInstance();
          user = service.register(user);
          if (user.getIduser() == 0) {
              context.addMessage(null, new FacesMessage(
                  FacesMessage.SEVERITY_ERROR, "Registration failed", null));
              return null;
          }
          context.getExternalContext().getSessionMap().put("user", user);
          return "/index.xhtml?faces-redirect=true";
      }
  
      public String logout() {
          FacesContext.getCurrentInstance().getExternalContext()
              .invalidateSession();
          return "/index.xhtml?faces-redirect=true";
      }
  }
  </code></pre>
  
  <p>So while the Login button redirects me to the index and displays my login name and Register sends me to the index logged in as the new registered user, when I hit the Logout button I am left staring at <code>You are logged in as &lt;username&gt;</code>. There is only the index page and the registration page in the app</p>
  
  <p>Edit : the <code>index.xhtml</code>:</p>
  
  <pre><code>&lt;ui:composition template="/WEB-INF/xhtml/template.xhtml"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"&gt;
      &lt;ui:define name="title"&gt;All Times Classics - Welcome&lt;/ui:define&gt;
      &lt;ui:define name="content"&gt;
          &lt;h1 style="text-align: center"&gt;Welcome to All Times Classics&lt;/h1&gt;
          You can &lt;a href="register.xhtml"&gt;Register&lt;/a&gt; if you want to enjoy all
               the goodies we have to offer.
      &lt;/ui:define&gt;
  &lt;/ui:composition&gt;
  </code></pre>