This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes How can an attribute be duplicated in different scopes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "How can an attribute be duplicated in different scopes" Watch "How can an attribute be duplicated in different scopes" New topic
Author

How can an attribute be duplicated in different scopes

Sue Pillai
Ranch Hand

Joined: Nov 04, 2004
Posts: 89
With reference to HF's Servlets & JSP: (pg 385)

One of the reasons you use the scope implicit objects is to avoid a potential naming conflict. How can I have the same id for two useBean tags? When I try to set an attribute with the same name in request scope and session scope using:

<jsp:useBean id="person1" class="com.example.Person" scope="request">
Setting Request Attribute:
<jsp:setProperty name="person1" property="userName" />
</jsp:useBean>
Getting Request Attribute:
<jsp:getProperty name="person1" property="userName" />
<br>

<jsp:useBean id="person1" class="com.example.Person" scope="session">
Setting Session Attribute:
<jsp:setProperty name="person1" property="userName" />
</jsp:useBean>
Getting Session attribute:
<jsp:getProperty name="person1" property="userName" />

I am getting the foll error:

org.apache.jasper.JasperException:jsp.error.useBean.duplicate /CheckAction.jsp

I am trying to create a situation where there is a same-named attribute in 2 different scopes (say request and session) and figure out what it prints with and without using implicit objects. How do I go about it?

(I know I am being a hard-nut, but I just scored 50% in that chapter's mock and thought I'll be better off trying every silly thing out on the system )

Thanks.
[ February 06, 2006: Message edited by: Sue Pillai ]

Sue

SCJP 1.2
SCWCD 1.4
SCBCD 5.0
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

It doesn't like that the id attribute is the same in both tags.

A quick and dirty way to do your test is to use a quick scriptlet at the top of the page to set the values:

<%
request.setAttribute("sameName", "Request Value");
session.setAttribute("sameName", "Session Value");
%>


A good workman is known by his tools.
Sue Pillai
Ranch Hand

Joined: Nov 04, 2004
Posts: 89
Thanks Marc, It worked that way. I guess that is why scriptlets are useful sometimes, huhn!
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

For quick tests, yes.
 
Don't get me started about those stupid light bulbs.
 
subject: How can an attribute be duplicated in different scopes
 
Similar Threads
jsp:useBean
Do we consider Escape chars in Exam or not?
Bean standard action doubt
How type attribute in useBean be applied
Creating a bean in session scope