Forums Register Login

Confusion about "scope" attribute

+Pie Number of slices to send: Send
Hi,

I have a lot of confusion about the standard and custom actions that have a "scope" attribute; what happens if the "scope" is not specified. The following actions use the 'scope' attribute (please add more if i have missed any)

1. jsp:useBean :- if scope is not specified, then the action searches for the bean only in the page scope; if it doesnt find the bean in the page scope, it creates a new one

2. c:set var :- if scope is not specified, ???

3. c:remove var :- if scope is not specified, it removes the bean from ALL THE SCOPES!!

Help!!!
+Pie Number of slices to send: Send
Hi,

for c:set var="attr" , if the scope is not specified and then it searches for the attribute in all the scopes starting from page to request to session to application(not context but is an object of javax.servlet.ServletContext only) and first one found is given the value specified in either the body or the value attribute of c:set

If nothing specified by "attr" then a new attribute is created in the page scope (default behaviour)

Regards
Rishi Chopra
+Pie Number of slices to send: Send
I don't think that is correct... I think if scope is not specified in c:set, then the attribute is looked in the page scope only; if it is not there in the page scope, it is created in the page scope.
[ March 08, 2007: Message edited by: H Pathak ]
+Pie Number of slices to send: Send
Although HFSJ says that if scope is not specified then it will look at all the scopes, I don't think that is correct because:

1. I looked up the JSTL specs for c:set, and found (on pg 44) that "page" is the default value for the scope attribute; in other words, if we specify no scope, it defaults to scope=page, and this means that it will look only in the page scope.

2. I tried the following:

The output of first line was "JAWA" while the output of 2nd line was "JAWArerere"
+Pie Number of slices to send: Send
If scope is not set, the default will be the page scope. There's no such thing as looking in all scopes. The author of the previous post might have been confused with the "remove" tag.
+Pie Number of slices to send: Send
Hi,

Thanks for bringing up this point! I skimmed thru the JSTL spec just yesterday and missed it. But here it is today:


Syntax
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>

.....

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).
Syntax 3:
if target is a Map, remove the entry with the key identified by property.
if target is a JavaBean component, set the property to null.



This is from Null & Error handling under c:set. I also looked up PageContext.removeAttribute(name) from J2EE 1.4 spec:


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.

Parameters:
name - The name of the object to remove.
Throws:
NullPointerException - if the name is null



Remove, again from JSTL spec:


The <c:remove> action removes a scoped variable.
If attribute scope is not specified, the scoped variable is removed according to the semantics of PageContext.removeAttribute(varName). If attribute scope is specified, the scoped variable is removed according to the semantics of PageContext.removeAttribute(varName, scope).



Specs rule!

Cheers,
Anu
[ March 09, 2007: Message edited by: Anupama Ponnapalli ]
+Pie Number of slices to send: Send
Hi

From the all above ..

My conclusion is that below words are wrong right?

---------------------------------------------------------------------------
If scope is not set, the default will be the page scope. There's no such thing as looking in all scopes. The author of the previous post might have been confused with the "remove" tag.
---------------------------------------------------------------------------

i.e when removing any attribute if there is no scope attribute find in default scope"page" then it will look into request and session and application.. Right?

Please confirm this

ThanksInadvance
+Pie Number of slices to send: Send
Sorry ..the statement

If scope is not set, the default will be the page scope. There's no such thing as looking in all scopes. The author of the previous post might have been confused with the "remove" tag.

Is correct

Once again i feel sorry..
+Pie Number of slices to send: Send
Hi,

for c:set var="attr" , if the scope is not specified and then it searches for the attribute in all the scopes starting from page to request to session to application(not context but is an object of javax.servlet.ServletContext only) and first one found is given the value specified in either the body or the value attribute of c:set

If nothing specified by "attr" then a new attribute is created in the page scope (default behaviour)

==========
above statement was true but i assumed that the value is not null.
if value is null and scope is not spcified then the "attr"(in my example) will be removed from all the scopes ..
if value is null and scope is specified then the "attr" is removed if it exists in specified scope.

Regards
Rishi Chopra
+Pie Number of slices to send: Send
Okay, but then how can one explain the output of the code that I ran (5th post in this thread)?

Anyways, it doesn't matter to me strictly as I've cleared the exam, but it would be great that all doubts are cleared:

And 1 more thing regarding the spec:

JSP Spec for PageContext states that PageContext.setAttribute creates a new page-scoped attribute, but PageContext.removeAttribute will look in ALL scopes for removing the attribute... isn't that confusing
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1522 times.
Similar Threads
<jsp:invoke> for Tag files
question on el and standard action
jsp:useBean type requirements
page 448 Default scope of c:remove
Scopes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:28:00.