Meaningless Drivel is fun!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes c:set var Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "c:set var" Watch "c:set var" New topic
Author

c:set var

swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
Reference HFSJ Pg 445

"If the value of the object is null, the attribute is removed. (If scope is not specified it will start looking at page, request, and so on)"

Now if it does find an attribute at page level, i thought only the page level attribute is removed.

It came as a surprise to me that the attribute at all levels is removed.



Output
Print set values
>com.example.bean.MyBean@8fbecf<
>request<
>session<
>application<
Print new values
><
><
><
><

Is this a bug or it is supposed to work this way?
Using Tomcat 5.5.23
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16


If attribute scope is specified, the scoped variable is removed according to
the semantics of PageContext.removeAttribute(varName, scope).
Otherwise, the scoped variable is removed according to the semantics of
PageContext.removeAttribute(varName).


And if you look at PageContext.removeAttribute(varName) : Remove the object reference associated with the given name from all scopes. Does nothing if there is no such object.

So if you don't specify the scope, and the value is null, then the variable will be removed from all scopes.


[My Blog]
All roads lead to JavaRanch
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
Thanks!!! Should have looked at the API docs
Thanks though!!!
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
An Add-on

In the above jsp if we have a c:set without value, then only the pagescope is affected


Output is

><
>request<
>session<
>application<

if value is not provided then is the value considered to be an empty string as against null?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: c:set var
 
Similar Threads
Baffling NPE in <c:forEach
EL basic
c:remove
scriptlet works, scoped variable does not
Some Useful Notes