wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Confusion about 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 "Confusion about "scope" attribute" Watch "Confusion about "scope" attribute" New topic
Author

Confusion about "scope" attribute

H Pathak
Greenhorn

Joined: Dec 20, 2006
Posts: 28
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!!!


SCJP 1.4 - cleared<br />SCWCD 1.4 - cleared!!
Rishi Chopra
Ranch Hand

Joined: Mar 07, 2004
Posts: 108
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


Life is Beautiful Learn to See it.........
H Pathak
Greenhorn

Joined: Dec 20, 2006
Posts: 28
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 ]
H Pathak
Greenhorn

Joined: Dec 20, 2006
Posts: 28
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"
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Anupama Ponnapalli
Ranch Hand

Joined: Jun 12, 2006
Posts: 66
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 ]
madhav changala
Ranch Hand

Joined: Dec 20, 2005
Posts: 57
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


saivenumadhav
madhav changala
Ranch Hand

Joined: Dec 20, 2005
Posts: 57
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..
Rishi Chopra
Ranch Hand

Joined: Mar 07, 2004
Posts: 108
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
H Pathak
Greenhorn

Joined: Dec 20, 2006
Posts: 28
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Confusion about "scope" attribute