First, is this method correct? Is there a way without touching ServletAPI?
You can use ExternalContext#invalidateSession()
to terminate the session without having to grab the servlet API.
@ManagedBean @SessionScoped public class UserManager { private User current; public String logout() { FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); return "/home.xhtml?faces-redirect=true"; }
what will happen to my current bean session? since even the bean itself is stored in an HttpSession?
It will still be available in the current response, but it will no longer be in the next request. Therefore, it is important that the redirect (new request) is triggered after cancellation, otherwise you are still showing data from the old session. Redirecting can be done by adding faces-redirect=true
to the result, as I did in the above example. Another way to send a redirect is to use ExternalContext#redirect()
.
public void logout() throws IOException { ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext(); ec.invalidateSession(); ec.redirect(ec.getRequestContextPath() + "/home.xhtml"); }
In this context, its use is doubtful, since using navigation results is simpler.
BalusC Apr 11 '11 at 11:25 2011-04-11 11:25
source share