aspose file tools*
The moose likes Servlets and the fly likes session.removeAttribute()    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "session.removeAttribute()    " Watch "session.removeAttribute()    " New topic
Author

session.removeAttribute()

gurusamy sengodan
Ranch Hand

Joined: Oct 13, 2005
Posts: 41
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()?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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
Chaitanya Devavarapu
Greenhorn

Joined: Dec 06, 2006
Posts: 19
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.
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
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.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
vishnu
Greenhorn

Joined: Nov 28, 2006
Posts: 5
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.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61598
    
  67

"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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

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,


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61598
    
  67

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

Joined: Dec 06, 2005
Posts: 1321

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61598
    
  67

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

Joined: Dec 06, 2005
Posts: 1321

Thanks bear,

now it's completely clear to me..

Thanks again..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: session.removeAttribute()