Using JSF 2.2

How to close a JSF session ?

SessionScoped beans are to be used inside a web application context. When a bean is configured as 
SessionScoped this means that an instance of this bean will exist per HTTP session.  

Sample Code: 
@Named("jPATestBean")
@SessionScoped
public class JPATestBean implements Serializable
  {
  ...
  public String sessionLogout()
      {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        setRunTimeInfo("sessionLogout() is closing JSF session");
        return "/index.xhtml?faces-redirect=true";      
      }
   ... 


Method invalidateSession() will close your current JSF session. Your Beans related data is 
still be accessible in the current response, but it will not be there anymore in the next 
request. Thus it's important that a redirect (a new request) is fired after invalidate, 
otherwise you're still  displaying data from the old session. A redirect can be done by 
adding faces-redirect=true.

Leave a Reply

Your email address will not be published. Required fields are marked *