Mango
Mango

Reputation: 660

session invalidate in JSF2.0

I'm using JSF2.0 with jsp.I'm trying to incorporate session invalidation in my project.I've tried using the following code.

<h:commandButton value="Logout" action="#{bean.logout}" </h:commandButton>

and my bean class contains the following method

   public class Bean{
    public String logout(){
      FacesContext context = FacesContext.getCurrentInstance();
      HttpSession session = (HttpSession)context.getExternalContext().getSession(false);
      session.invalidate();
      return "login";
    }
   }

where string login redirects to login page.

my project has several pages which includes a header page..when i tried the above way...it was working fine when i click on logout from the very first page...If i try the same after going to other pages, its not logging out.Can anyone help me on this...is this the way we invalidate a session here???

UPDATE

I also tried using "*" in the navigation rule so that each page can be redirected to Login...but still the issue was same

Upvotes: 0

Views: 2903

Answers (3)

Muhammad Salman
Muhammad Salman

Reputation: 1

Use remoteCommand tag and on logout call this remoteCommand using JavaScript and provide the implementation in managedBean for the logout and the implmentation is ok that you paste here just need enter code hereto redirct to login page, or you can use again JavaScript to redirect to login page.

<h:commandButton value="Logout" onclick="closeSession();" </h:commandButton>

<p:remoteCommand name="closeSession"
action="#{jobController.logout}">

public class Bean{
    public String logout(){
          FacesContext context = FacesContext.getCurrentInstance();
          HttpSession session = (HttpSession)context.getExternalContext().getSession(false);
          session.invalidate();
          return "login?faces-redirect=true";
   }
}

Upvotes: 0

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

Did you try this -

return "/login?faces-redirect=true";

If the view login is in the root directory.

Otherwise if it's in some other folder then as follows -

//if it's in the folder - folder1
return "/folder1/login?faces-redirect=true"

Notice / at the beginning of the outcome.

Upvotes: 0

Chris
Chris

Reputation: 8034

Try

return "login?faces-redirect=true"

as outcome so the browser does not use the same request for the login-page, which has the session still active.

Upvotes: 2

Related Questions