It's not a secret anymore!*
The moose likes Servlets and the fly likes Check that a session attribute exists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Check that a session attribute exists" Watch "Check that a session attribute exists" New topic
Author

Check that a session attribute exists

Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
I have in my web app a couple servlets. The first servlet sets a session attribute. Sometimes it doesn't set a session attribute. Is there a way to check if an attribute exists? Some sort of ???

Or should I just always set the attribute to "" if it is not needed? That way it is always created and then I can just check for no value?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410



Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
Thanks Ben. I though I could do it gonna save me lines and lines of code
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you don't already have a link to the API on your desktop, you should.

See:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSession.html#getAttribute(java.lang.String)


getAttribute

public java.lang.Object getAttribute(java.lang.String name)

Returns the object bound with the specified name in this session, or null if no object is bound under the name.

Parameters:
name - a string specifying the name of the object
Returns:
the object with the specified name
Throws:
IllegalStateException - if this method is called on an invalidated session





Vishal Kumar Shaw
Greenhorn

Joined: Mar 27, 2011
Posts: 1
Bryce Martin wrote:I have in my web app a couple servlets. The first servlet sets a session attribute. Sometimes it doesn't set a session attribute. Is there a way to check if an attribute exists? Some sort of ???

Or should I just always set the attribute to "" if it is not needed? That way it is always created and then I can just check for no value?





May be you should use

String s = session.getAttribute("myAtribute").toString();
if ( s.equals( "null" ){
...........
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Vishal Kumar Shaw wrote:May be you should use :
[b]String s = session.getAttribute("myAtribute").toString();


No. That will cause a Null Pointer Exception if the attribute (scoped variable) is not set.





P.S. Your desire to help is appreciated, but why are you answering a question (an old one at that) that has already been correctly answered?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vibhatha Lakmal
Greenhorn

Joined: Aug 29, 2012
Posts: 4
Oh! you saved my time.... Thanks...


all I care is learn something from everyone...
"If there is a day, I could live, Single breath,I could take I take all the things into ma head "--edited Laid to Rest-Lamb of God...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Check that a session attribute exists
 
Similar Threads
Problems with sessions
TypeCast from String to HashMap
Session in JSP
JSF Filter prevent direct access certain page
how we will check session objects in logic:present tag