Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding scope of <c:set> tag

 
Isha Garg
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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!
 
Isha Garg
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad I could help! :-)
 
Rohini Sahuji
Greenhorn
Posts: 28
1
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rohini Sahuji
Greenhorn
Posts: 28
1
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You for the link, Abimaran !
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohini Sahuji wrote:Thank You for the link, Abimaran !

You are Welcome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic