• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

removeAttribute() question

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you've only forwarded the request to another JSP, then you'll get the same bean.
 
Bear Bibeault
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic