Skip to main content

Correct way to terminate a session / JSF 2 ?

3 replies [Last post]
softwaresimian
Offline
Joined: 2010-03-20
Points: 0

When logging out of an application, I wanted to remove the session associated with the user. To do this I ended up with the following:

...
public String testAction()
{
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "/login.xhtml";
}

This has the desired effect in destroying the session and returning to the login page, but I generate an exception in the background:

SEVERE: javax.faces.FacesException: No active contexts for scope type javax.context.RequestScoped
javax.faces.FacesException: No active contexts for scope type javax.context.RequestScoped
at com.sun.faces.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:136)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:115)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:135)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:309)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)

Two questions:

1. Is this the correct approach ?, if not, what do you recommend ?
2. If yes, how do I deal with the exception

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
razib
Offline
Joined: 2010-01-28
Points: 0

See if my answer helps you:
1. Yes this is a correct approach to remove the session (as far as I know)
2. Once session scope has been invalidated, redirect the user:

public String testAction()
{
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return [b]"/login?faces-redirect=true";[/b]
}

softwaresimian
Offline
Joined: 2010-03-20
Points: 0

Hiya

Thanks for the reply. Unfortunately I still get the exception thrown!

jkva
Offline
Joined: 2005-04-06
Points: 0

You can try to redirect manually, like this:

public String testAction()
{
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.invalidateSession();
ec.redirect("login.xhtml"); // Or whatever servlet mapping you use
// redirect() invokes FacesContext.responseComplete() for you

return null;
}