The moose likes JSF and the fly likes How to destroy the Object of a SessionScoped bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How to destroy the Object of a SessionScoped bean " Watch "How to destroy the Object of a SessionScoped bean " New topic

How to destroy the Object of a SessionScoped bean

Dushyant Agarwal
Ranch Hand

Joined: Oct 14, 2007
Posts: 75
I have been building an application that requires me to Keep exactly one session scoped bean. But I also have another war file to be navigated to
at the end of this application. I want to destroy the Session Scoped Bean's object at the time of navigation to the second war file(i.e. clear up the memory allocated to the session scoped object ).

How can I do this ???

Would System.gc() help???

I have doubts because System.gc() doesn't guarantee garbage collection upon calling the method!!!
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
If you don't need the session you could do session.invalidate() which also removes all objects from a session

I you only want the remove the attribute you could use the removeAttribute method of the session


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Dushyant Agarwal
Ranch Hand

Joined: Oct 14, 2007
Posts: 75
Thanks for the reply!
But I have got a managed bean whose managed bean scope is session.
Whtaever you have suggested would have been appropriate if I had been using

HttpSession ref=(HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession();


However, I am declaring a variable "var" in the class (let us say )SessionBean1. Beecause of session scope the value of var will persist across the session. That will serve the same purpose as

HttpSession ref=(HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession();


However I am wanting to know how could I be able to destroy the Object of SessionBean1, to free the allocated memory.
zel branco

Joined: Jan 04, 2008
Posts: 3
FacesContext context = FacesContext.getCurrentInstance();
Dushyant Agarwal
Ranch Hand

Joined: Oct 14, 2007
Posts: 75
Hi,Thanks for the reply!

However it is not working.

See after I got your reply, I tried the following:
I built a class SessionBean1 as following:

public class Sessionbean1{
private str="testing for session destroy";

//gettter and setter of str

In the faces-Config I had set the scope of the above class
as session.

Now in the backing bean of a page Page1.jsp that conatins
a h:commandButton I placed he following code in the action method of the

public String button1Action(){
System.out.println(<obj>.getStr()); //here obj retreives the
//session scoped object of
//SesionBean1 class.

Probably this code should have given a nullPointer Exception because


should have destroyed the object of SessionBean1.But it did not.

My problem still stands .Please help!!
[ January 07, 2008: Message edited by: Dushyant Agarwal ]
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
However it is not working.

Did you try it with "#{SessionBean1}"?


[ January 08, 2008: Message edited by: Richard Green ]

MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Dushyant Agarwal
Ranch Hand

Joined: Oct 14, 2007
Posts: 75
Thanks for reply .
I finally got to get it working with "SessionBean1".
But I wanted to know whether after we execute


if we try to access the reference of Session scoped bean then should we get null as a result or a new reference of the same bean. I am getting a new reference of the Session scoped bean in that case.
I agree. Here's the link: http://aspose.com/file-tools
subject: How to destroy the Object of a SessionScoped bean
Similar Threads
Session is created as soon as jsf request is made to launch application in browser
Injecting a session bean into an application bean
Spring beginner needs help with design issue
JEE5 bean and war deployment
exception and error while importingwar in websphere applicationserver6