This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

session.removeAttribute()

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have added one field by using session.setAttribute("DBID",dbName);
Once use it
Is it necessary to remove the attribute by using session.removeAttribute()?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you know you're done with it and won't need it again, it's not a bad idea to explicitly remove it; especially if it's large or contains references to many other objects.

If you don't remove it, it will be removed when the session is destroyed.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you are done using an attribute, it's always polite to clean it up and remove it from the session.

I did notice the DBID as the attribute. If the attribute being put in the session is used universally for all users accessing that application, you might want to consider putting it into the ServletContext, as opposed to the session. With a session, the attribute is duplicated for every user. If it's put into the context, every user shares the same value, which is very efficient.

Just a thought.

-Cameron McKenzie
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't have future requirement with that object, you can remove it
by using
session.removeAttribute();
Its not a bad practice.

If you do not remove it at present also,there is no problem.

Because you are storig the object in session, whenever your session
time-out is over, then the session object is automatically removed by
the web-container.
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its definitely a good idea to remove it once its no longer in use. House-keeping should be done else you may run out of memory.

If you're dealing with clustering, the more objects you store in the session, the more work the server has to do to replicate the session. And all these add overheads to your server and could affect your application performance.

Unless very necessary, try putting it into the request object, else your application may not be that scalable.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its good habit to delete that all but it is not compalsary to do that.when ever you are session is destroy that session will also removed.

Thanks
vishnu.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"vishnu",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted, often without warning

thanks,
Dave
 
Sheriff
Posts: 67752
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
"vishnu", that was your third and final warning. Change your display name as directed prior to your next post or your account will be disabled.

bear
JavaRanch Sheriff
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
it's not a bad idea to explicitly remove it



what about the session.invalidate() method ? can we use this instead of removeAttribute() please give us some valuable suggestion to handling with session.

Thanks in Advance,
 
Bear Bibeault
Sheriff
Posts: 67752
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
session.invalidate() will invalidate the whole session, including all scope variables set within it. Sort of like using dynamite to open a jar of pickles.
[ December 10, 2006: Message edited by: Bear Bibeault ]
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks bear,

so you mean to say session.removeAttribute() is more better choice instead using session.invalidate() method.

so where this method is useful?
 
Bear Bibeault
Sheriff
Posts: 67752
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 Saif uddin:
so you mean to say session.removeAttribute() is more better choice instead using session.invalidate() method.



Invalidating the whole session in order to remove an attribute isn't just "not better", it's destructive and incorrect.

so where this method is useful?



That's a completely diffrernt question than this topic, so please start a new topic if you have questions about session invalidation.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks bear,

now it's completely clear to me..

Thanks again..
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic