File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes c:set var question 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 question" Watch "c:set var question" New topic
Author

c:set var question

Yogesh Hingmire
Ranch Hand

Joined: Dec 06, 2005
Posts: 61
If i use a <c:set var = "Fido" value = null/> and i have two attributes one in page and other in request scope.
Would the execution of this tag remove the Fido attribute from page scope as well as the request scope?

Thank You
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

I don't think you can say value=null. And if you say value="null", well you'll get a String with the characters n, u, l, and l.

value="${null}" might work.

It will only change one scope's value - the first one it finds. Since you did not specify a scope, it will first look in the default page scope (which it finds) and that should be the only one effected.


A good workman is known by his tools.
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
I don't think you can say value=null
No, indeed not! If you want to remove something from a scope, use <c:remove />:In that case it'll remove the attribute called "Fido" from all scopes (by the semantics of PageContext's removeAttribute method).

Back to your original question, if you use:The value of the "Fido" page-scoped attribute will be set to "SomethingElse", overwriting any previous value stored in that attribute. It will only affect the page scope here as you haven't supplied a scope explicitly. <c:set /> operates according to the semantics of PageContext's setAttribute method when no scope is supplied.


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Yogesh Hingmire
Ranch Hand

Joined: Dec 06, 2005
Posts: 61
Thanks Marc/Charles.
I understand, that <c:remove> is the way to go for attribute removal, but my question is HFSJ mentions that <c:set has the effect of removing an attribute from a scope, when you set it to a ${null} value.
From Marc's answer it seems it will only remove the attribute from the first scope it finds it in.
so say for eg : if i have this :

<c:set var = "Fido" value = "${null}"/>.

I have Fido in request and session scope.

per Marc, it will only remove the attribute in request scope and not the session scope.

Please let me know if my understanding is correct.

Thanks
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
HI,

<c:set var = "Fido" value = "${null}"/>.

This tag removes all the attribute Fido from all scopes.

If the attribute scope is specified, the scope variable is removed according to the semantics of PageContext.removeAttribute(varName, Scope). That is the attribute is removed from particular scope.

If the attribute scope is NOT specified, the scope variable is removed according to the semantics of PageContext.removeAttribute(varName). That is the attribute is removed from ALL scope.

Alternately, As per Charles said, you can use

<c:remove var="Fido" />

for the same effect.


Thanks
[ July 28, 2006: Message edited by: Narendra Dhande ]

Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

That didn't sound right to me, so I had to test it.

The reason it didn't sound right is that when you use c:set to replace a value without specifying scope it will only replace the first one it finds. Why should a value that evaluates to null act differently? But it does!


That code gave this output:
aaa bbb ccc ddd --- zzz bbb ccc ddd --- ---

Totally weird!!!
[ July 28, 2006: Message edited by: Marc Peabody ]
Yogesh Hingmire
Ranch Hand

Joined: Dec 06, 2005
Posts: 61
Thanks Narendra/Marc,

Can we conclude that <c:set> behaves differently when you set by specicfying some value as opposed to when you set by specifying an expression that evaluates to null ?

Yogesh
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Yes. I believe my test proved that.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Thanks Marc for your testing result,

I had not tested the <c:set> behaviour for null values. But I read it from the JSTL 1.1 spec. There is one seperate para on this behaviour.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: c:set var question