This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt regarding scope of <c:set> tag 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 "Doubt regarding scope of <c:set> tag" Watch "Doubt regarding scope of <c:set> tag" New topic
Author

Doubt regarding scope of <c:set> tag

Isha Garg
Greenhorn

Joined: Mar 12, 2010
Posts: 29
There is a question in Headfirst at page 457 related to <c:set> tag

Q: If I don’t specify a scope, does that mean it will find attributes that are ONLY within page scope, or does it do a search beginning with page scope?

A: If you don’t use the optional “scope” attribute in the tag, then the tag will only look in the page scope space. Sorry, you will just have to know exactly which scope you are dealing with.


I have a doubt related to this.
I feel it does search for the attribute in page scope then request then session then....

I checked with following example:

In my servlet I have
request.getSession().setAttribute("genre", "Ambient");

In my jsp:

<c:set var="genre" value="newGenre" />
<br/>
value of genreVar: ${genre}


I get the result as
value of genreVar: newGenre

So, if I have not specified scope here, according to author it should just search in pageScope only but in my case my genre attribute was in session, so it has searched in session and replaced the value in session.

Can you please clear my understanding....

Thanks.






Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

@Isha, are you sure it is replacing the session scope variable, or maybe it is just creating new variable in page scope with the same name?

Maybe try printing out values of "genre" variable in all scopes.

Cheers!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Isha Garg
Greenhorn

Joined: Mar 12, 2010
Posts: 29
yes you are right, it is creating a new attribute at page scope.

I checked this and following are the values:

value of pageScope.genre:::: newGenre
value of requestScope.genre:::
value of sessioncope.genre:::: Ambient
value of applicationScope.genre:::

So the conclusin is if no scope is specified, it just considers pageScope, does not look at othe scopes.

Thanks for your help.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Glad I could help! :-)
Rohini Sahuji
Greenhorn

Joined: Jul 06, 2007
Posts: 20

Thanks Isha,
I too got confused about this.

Because in HFSJ on page 455 its mentioned there
(If you don’t specify a scope, it will start looking at page, then request, etc.)
So as per my understanding its true for removing only..... means only when value is null, it will look at page the request ....etc? and delete the attribute...
otherwise it will look in page scope only.(default scope)
Correct me If I am wrong!


Thank You!


SCJP
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Welcome to JR Rohini Sahuji!

Yes, you are correct! Have a look on the JSTL Specification for the details, the default scope will be underlined in the corresponding tags!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Rohini Sahuji
Greenhorn

Joined: Jul 06, 2007
Posts: 20

Thank You for the link, Abimaran !
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Rohini Sahuji wrote:Thank You for the link, Abimaran !

You are Welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding scope of <c:set> tag