File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes clear session scoped variable values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "clear session scoped variable values" Watch "clear session scoped variable values" New topic

clear session scoped variable values

sri pathi

Joined: Jul 15, 2008
Posts: 28
Hi All,
I have a problem now. My application has a sequence of 7 screens and at the end of 7th screen there is a Submit link which saves the values to database. I have the managed bean for all the pages in session scope so that the form retains the values even after the user navigates to say 6th page and comes back to first page. So now when the user submit the values finally I would like to clear the managed bean values in the session scope. Each managed bean has about 10 - 20 fields.
Is there an efficient way to do this? Any input would be greatly appreciated.
Thanks everyone for your time.
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

This may help you.

web page

My Blog SCJP 5 SCWCD 5
sri pathi

Joined: Jul 15, 2008
Posts: 28
The link looks like what I wanted to do, but I could not get it to work.
I even tried to get the values from session map by using FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("ManagedBeanClassName")
Is that how we get an item from a session map?
prasad kolla

Joined: Jul 19, 2008
Posts: 12
It can be some thing like this. you can remove the mpb from the session, bean name will be the one you specify in your faces-config.xml.

Class.setVariableValue("#{" + beanName + "}", null);

public static void setVariableValue(String facesVarName, Object newValue) {
FacesContext ctx = getFacesContext();
ValueBinding bind = ctx.getApplication().createValueBinding(facesVarName);

Class bindClass = bind.getType(ctx);
if(newValue == null || bindClass.isInstance(newValue))
bind.setValue(ctx, newValue);
bind.setValue(ctx, newValue);
A. Dusi
Ranch Hand

Joined: Sep 27, 2004
Posts: 114
You can have some resetting methods in each managed bean and call each one of them on click of Submit. You should be able to access one managed bean from another. I dont know if this is a good practice, but every property that I have in a managed bean, I also have it in the reset method of the managed bean.
But do consider where the submit button should take to; will it still be showing the data present on these 7 pages?
If this is the case you are better off calling those reset methods while navigating away from these 7 pages(like back to Main Menu or some other unrelated page). As an alternate, you could also call these reset methods at the time of starting the 7 pages workflow.

Note that above methods only reset the properties in the managed bean; the managed bean itself will still be in session.
[ December 02, 2008: Message edited by: A. Dusi ]
I agree. Here's the link:
subject: clear session scoped variable values
It's not a secret anymore!