• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HFSJ 2ed - about <c:set>

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
page 457 has Dumb questions one of which says:

Q: If you do not specify a scope...
A: If you do not use the optional "scope" attribute in the tag, then the tag will only look in the page scope space....


what do they mean ?
I tried to put a var in the session/request scope and I was able to access/change a property of it using <c:set target flavor without specifying the scope as "session" or "request".
It's confusing.
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aj,

According to the specs (JSP2.0) the default scope is "page"

Syntax 1: Set the value of a scoped variable using attribute value
<c:set value=”value” var=”varName” [scope=”{page|request|session|application}”]/>

Syntax 2: Set the value of a scoped variable using body content
<c:set var=”varName” [scope=”{page|request|session|application}”]>
body content
</c:set>

Syntax 3: Set a property of a target object using attribute value
<c:set value=”value” target=”target” property=”propertyName”/>

Syntax 4: Set a property of a target object using body content
<c:set target=”target” property=”propertyName”>
body content
</c:set>


So when setting a property "twee" in the session scope

and changing it without specifying the scope should not be possible (as the default scope is page)

However if you evaluate the property it has changed into 2


The tomcat container has its own interpretation of the specs: it uses the pageContext.findAttribute() to locate the attribute and change it from "1" to "2".

Conclusion: don't always rely on what the container does: verify the behaviour in the specifications!

Regards,
Frits
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Frits,

The container is following the specs here.

The session scoped attribute's value has not changed to 2. ${sessionScope.twee} would still display 1.


@Aj,

can you post the whole question ? You might not be modifying the request/session scoped variable, but creating and modifying a page scoped variable.

Vishwa

 
Aj Deschanel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually Fritz's code displays 2, which looks like tomcat doesn't follow the spec

my code does this:


The result is
George Nick

BTW: can somebody point to the spec lines where it says that you need to specify the scope your var is in ?!
 
Aj Deschanel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JBOSS 5.1.0 gives the same result as tomcat for the examples above.
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aj,

The results look perfectly fine to me.

Firstly let me explain the scoped variables with an example-



is displayed as


Session Scoped Variable 2
Page Scoped Variable:
Search and display the least scoped variable: 2

Session Scoped Variable 2
Page Scoped Variable: 1
Search and display the least scoped variable: 1




Then,


If you do not use the optional "scope" attribute in the tag, then the tag will only look in the page scope space



Guess, this explanation was given for the useBean tag...

Consider this code in One.jsp



and consider this code in Two.jsp



And One.jsp is accessed followed by Two.jsp

In this case, when the One.jsp is accessed initially, a session scoped variable 'user' is created and it is assigned with George. The page displays 'George'

Then when Two.jsp is accessed, it displays 'Nick'

You might've expected the setProperty line in Two.jsp to not be executed since there *is* already a session scoped variable present by that name.

But the line gets executed because the scope attribute is not specified in the useBean tag of Two.jsp.
It checks only the page scope, finds that the variable is not present and hence executes the nested setProperty statement.

If in Two.jsp scope is specified as session, then the nested setProperty would not have been executed.


HTH,
Vishwa
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, the scope matters with the "var" attribute of the <c:set> tag. If you use, target attribute, then you've to use the real object(bean or map), for that, we use EL, Scriptels, and <jsp:attribute>. If you use EL, it will search in every scope, we can't mix it with scope attribute of the <c:set> tag!
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vishwanath

The container is following the specs here.
The session scoped attribute's value has not changed to 2. ${sessionScope.twee} would still display 1.


hmmm I see..., you are correct: good explanation ! ( I still get mixed up now an then )

Regards,
Frits

 
Aj Deschanel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot everybody.
Looks like whatever they say in the book is related to c:set with var attribute.
It makes 100% sense since with target it uses EL and EL knows to search all scopes, as Abimaran already mentioned.

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.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aj Deschanel wrote:Thanks a lot everybody.
Looks like whatever they say in the book is related to c:set with var attribute.
It makes 100% sense since with target it uses EL and EL knows to search all scopes, as Abimaran already mentioned.

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 find the answer to this question to a bit confusing when compared with the 'Bing' in Head First on page 455 which says:


If the value evaluates to null, the variable will be removed. If there is an existng attribute with the same name, the attribute will be removed.
If you don't specify a scope, it starts looking at page,then request etc.



Question 1
If both, the answer to the dumb question and the Bing hold true, can we say that:

a. If the var is not found AND the scope is not specified, it will search only in the page scope and create an attribute if it does not previously exist.
b. If the value is null AND the scope is not specified, the attribute would be searched in all the scopes (page,request,session and so on) and removed wherever it is found first.

Question 2

If answer to question (b) above is true, will it throw an exception if the attribute is not found anywhere in any of scopes?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shalabh,

a. If the var is not found AND the scope is not specified, it will search only in the page scope and create an attribute if it does not previously exist.


Correct

If the value is null AND the scope is not specified, the attribute would be searched in all the scopes (page,request,session and so on) and removed wherever it is found first.


Correct, because of the following part of the jstl spec:

4.3 <c:set>
... removed ...
If value is null
- Syntax 1: the scoped variable defined by var and scope is removed.
+ 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).


If you read the API description of PageContext.removeAttribute(varName).

public abstract void removeAttribute(String name)
Remove the object reference associated with the given name from all scopes. Does nothing if there is no such object.


The underlined part answers your Question 2.

Regards,
Frits
 
Shalabh Vyas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Frits,

Thanks.That answered both my questions.
 
reply
    Bookmark Topic Watch Topic
  • New Topic