Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

removeAttribute() question  RSS feed

 
Steven Marco
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a JSP page that has:

<jsp:useBean id="myBean" scope="session" class="...">

Then a bean object is created right?

Now if I have a Servlet that contains the code:

request.getSession().removeAttribute("myBean")

Does this remove the bean object?
[ October 01, 2006: Message edited by: Steven Marco ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steven Marco:
If I have a JSP page that has:

<jsp:useBean id="myBean" scope="session">

Then a bean object is created right?


Not in this case because you did not supply a class attribute.


request.getSession().removeAttribute("myBean")

Does this remove the bean object?


That code will remove a scoped variable named myBean from the session regardless of how it got there.
 
Steven Marco
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


That code will remove a scoped variable named myBean from the session regardless of how it got there.


So that means "myBean" object still exists somewhere? (if I have put class="...." in)
[ October 01, 2006: Message edited by: Steven Marco ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Removing the attribute from the session (or any other scope) only removes the reference to the object from that scope. At that point, the object is subject to normal Java garbage collection.
 
Steven Marco
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Removing the attribute from the session (or any other scope) only removes the reference to the object from that scope. At that point, the object is subject to normal Java garbage collection.


Thanks! If I use:

request.getSession().getAttribute("myBean")

than I would be able to reference it again?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Remember that even if the instance is still somewhere in memory, you have removed its reference from the session. You cannot get it back.
 
Steven Marco
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:
No. Remember that even if the instance is still somewhere in memory, you have removed its reference from the session. You cannot get it back.


OK, if I did not use the removeAttribute() in my Servlet, then if I use this Servlet to forward to another JSP with the code:

<jsp:useBean id="myBean" scope="session" class="...">

Am I referring to the same bean that I created in the first JSP page or it's different?
[ October 01, 2006: Message edited by: Steven Marco ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you've only forwarded the request to another JSP, then you'll get the same bean.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steven Marco:

Thanks! If I use:

request.getSession().getAttribute("myBean")

than I would be able to reference it again?


I think Satou misunderstood your question. The answer is yes, getting the attribute (scoped variable) does not affect it in any way.
[ October 01, 2006: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steven Marco:

Am I referring to the same bean that I created in the first JSP page or it's different?


It will be the same and available until you remove it or the session times out.
[ October 01, 2006: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!