title: Login Logout in JSF 2
body: <p>I tried to do login and logout method refer this article <a href="http://www.itcuties.com/j2ee/jsf-2-login-filter-example/" rel="nofollow">http://www.itcuties.com/j2ee/jsf-2-login-filter-example/</a>
  Login method is fine but logout doesn't work!</p>
  
  <p>LoginBean.java</p>
  
  <pre><code>@ManagedBean
  @SessionScoped
  public class LoginBean implements Serializable {
  
      @EJB
      private UsersFacadeLocal usersFacade;
  
      public LoginBean() {
      }
  
      //
      public String doLogin() {
          String redirect = "";
          if (usersFacade.loginUser(email, password)) {
              loggedIn = true;
              redirect = navigationBean.redirectToMain();
          } else {
              FacesContext facesContext = FacesContext.getCurrentInstance();
              facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Username or password is incorrect", null));
              redirect = navigationBean.redirectToLogin();
          }
          return redirect;
      }
  
      //
      public String doLogout() {
         ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
          HttpServletResponse response = (HttpServletResponse)ectx.getResponse();
          HttpSession session = (HttpSession)ectx.getSession(false);
          session.invalidate();
          loggedIn = false;
          return navigationBean.redirectToLogin();
      }
      //
      private String email;
      private String password;
      private boolean loggedIn;
      @ManagedProperty(value = "#{navigationBean}")
      private NavigationBean navigationBean;
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      public boolean isLoggedIn() {
          return loggedIn;
      }
  
      public void setLoggedIn(boolean loggedIn) {
          this.loggedIn = loggedIn;
      }
  
      public NavigationBean getNavigationBean() {
          return navigationBean;
      }
  
      public void setNavigationBean(NavigationBean navigationBean) {
          this.navigationBean = navigationBean;
      }
  }
  </code></pre>
  
  <p>LoginFilter.java</p>
  
  <pre><code>public class LoginFilter implements Filter{
  
      @Override
      public void init(FilterConfig filterConfig) throws ServletException {
      }
  
      @Override
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
          HttpServletRequest req = (HttpServletRequest)request;
          HttpServletResponse res = (HttpServletResponse)response;
          LoginBean session = (LoginBean)req.getSession().getAttribute("loginBean");
          String url = req.getRequestURI();
          if(session == null || session.isLoggedIn() == false)
          {
              if(!url.equals("/login.xhtml"))
              {
                  res.sendRedirect(req.getContextPath() + "/login.xhtml");
              }
              res.sendRedirect(req.getContextPath() + "/login.xhtml");
          }
          else
          {
              chain.doFilter(request, response);
          }
      }
  
      @Override
      public void destroy() {
      }
  </code></pre>
  
  <p>Web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;LoginFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;ejb.DAO.LoginFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;LoginFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;faces/main.xhtml&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>After logged in, when I click logout button it's redirect to login page. but when I copy and paste the main page url to a new tab in browser the session is still there. </p>
  
  <p>Any suggestion. Thanks in advance</p>