Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion about "scope" attribute

 
Harshit Patel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Rishi Chopra
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Harshit Patel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Harshit Patel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anupama Ponnapalli
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
madhav changala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Harshit Patel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic