Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear an object of the session scoped managed bean?

I am developing a login based application in JSF with primefaces. In that I kept the logged user info in session scoped managedbean and I need to clear that details when he logged out, So How to clear those details which are in SessionScoped ManagedBean object?

like image 396
Arun Avatar asked Feb 21 '13 10:02

Arun


People also ask

How to destroy session in jsf?

You can destroy the session by ExternalContext#invalidateSession() . E.g. Remember to send a redirect afterwards, because the session objects are still available in the response of the current request, but not anymore in the next request.

What is the default scope of a managed bean?

Normally the default scope is the Request scope.

What is managed bean in JSF Javaserver faces?

A managed bean is created with a constructor with no arguments, a set of properties, and a set of methods that perform functions for a component. Each of the managed bean properties can be bound to one of the following: A component value. A component instance.


1 Answers

You need to invalidate the current session by calling the following function in your action method:

FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

Also, as the session scoped beans are trashed buring the subsequent request, be sure to send a redirect:

FacesContext.getCurrentInstance().getExternalContext().redirect("/login.xhtml");

Or, simply return a navigation case outcome from your method:

return "login.xhtml?faces-redirect=true";

In case you don't want to invalidate the session and, effectively, retaining your session scoped beans (which is a bad practice in my opinion), just nullify all of the user data (which was hopefully collected in one session scoped managed bean) in the logout method (you may need to inject that bean in case the logout method resides in another session scoped bean).

like image 66
skuntsel Avatar answered Sep 20 '22 09:09

skuntsel