• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

The scope attribute

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there�
I have some doubts regarding JSP tag <c:set>. P. 447 of HFSJ book includes the following:


I you do not use the optional �scope� attribute in the tag, and you�re using �var� or �target�, the container will search scopes in the order you�ve come to expect-page � page, then request, then session, then application (context)
If you use �var� version without a scope, and the container can�t find an attribute of that name in any of the four scopes, the container makes a new one in page scope.




Then please explain to me why the following code is not working correctly:

Servlet code

JSP code

The JSP outputs :
Mark
Mark
Rather than
John
John

Why is that? The� name� variable exists in the request forwarded by the servlet. The JSP went on and created a new attribute in page scope and assigned the value �John� to it. In other words, <c:set> tag didn�t search all scopes. Only checked the default scope �page�, and when didn�t find a variable named �name� it automatically created one and refrained from checking other scopes.
A similar scenario applies to <jsp:useBean> tag, when the type attribute is used without �class�, <jsp:useBean> doesn�t search all scopes when the �scope� attribute is not used. Thus choosing the third option as an answer �as the book did� for the exercise in page 356 is incorrect.
Any clarification?
Hatim
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you use �var� version without a scope, and the container can�t find an attribute of that name in any of the four scopes, the container makes a new one in page scope.


I don't agree with this statement. If the value is not null, and you don't set the scope attribute, then a new value will be set in the default scope, which means in the page scope.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And also check the errata, as the text you are referring to has been revised. (about the target attribute)
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason why you are getting "Mark" printed out because you didn't specify the scope attribute when setting the variable to a new value thus it defaults to page scope. Here is the definition of <c:set..> from jstl specification.

<c:set var=�varName� [scope=�{page|request|session|application}�]
value=�value�/>
the attribute scope is optional. If it is specified, its value must be one of page,
request, session, or application. The default value is page.


So, using ${pageScope.name} results to "John" and <c:set var="name" value="John" scope="request"/> results to Mark with ${requestScope.name}

Hope it clarifies your doubt.
 
hatim osman
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...
Thank you very much guys, all my doubts are cleared now. I have come along way in believing that the book is correct. But practical application made me think twice.
Hatim
 
reply
    Bookmark Topic Watch Topic
  • New Topic