Is there a way to check if an attribute exists
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 view plaincopy to
session.getAttribute != null
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?
if(null == session.getAttribute("myattribute"))......
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
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 view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
session.getAttribute != null
session.getAttribute != null ???
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" ){
...........
}