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 HFSJ 2ed - about <c:set> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "HFSJ 2ed - about <c:set>" Watch "HFSJ 2ed - about <c:set>" New topic
Author

HFSJ 2ed - about <c:set>

Aj Deschanel
Ranch Hand

Joined: Oct 20, 2009
Posts: 40
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.


SCJP 1.6, SCWCD 1.5
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1700
    
  25

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
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
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


Blog
Aj Deschanel
Ranch Hand

Joined: Oct 20, 2009
Posts: 40
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

Joined: Oct 20, 2009
Posts: 40
JBOSS 5.1.0 gives the same result as tomcat for the examples above.
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
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
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1700
    
  25

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

Joined: Oct 20, 2009
Posts: 40
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.

Shalabh Vyas
Greenhorn

Joined: Dec 17, 2008
Posts: 10
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?


OCP-Java 6 (100%),OCWCD-JavaEE 5
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1700
    
  25

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

Joined: Dec 17, 2008
Posts: 10
Hi Frits,

Thanks.That answered both my questions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HFSJ 2ed - about <c:set>