This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Bean related exercise question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Bean related exercise question" Watch "Bean related exercise question" New topic
Author

Bean related exercise question

Rajendra Nath
Greenhorn

Joined: Nov 09, 2005
Posts: 27
DOUBT ON BEAN RELATED STANDARD ACTION EXERCISE(page 416 0f HFSJ)
Given:
<jsp:useBean id="person" type="foo.employee">
<jsp:setProperty name="person" property="name" value="Fred" ?>
</jsp:useBean>
Name is:<jsp:getProperty name="person" property="name" ?>
..............................................................................
[Here class Employee extends an abstract class Person]

and the related servlets are:

(1) foo.Person p=new foo.Employee();
p.setName("Evan");
request.setAttribute("person",p);


second servlet

(2) foo.Employee p=foo.Employee();
p.setName("Evan");
request.setAttribute("person",p);

The answers given are:

(1) fails at request time. the person attribute is stored at request scope.

(2)
and the second one works fine and prints out "evan".

But i find them contradictory answers. Since, both attributes are request scope. Both should fail.
Saurabh Kumar
Ranch Hand

Joined: Aug 21, 2006
Posts: 56
Hi,
You are right, I tried the code with different combinations.

The following combination gives error @ runtime in both cases:
Saurabh Kumar
Ranch Hand

Joined: Aug 21, 2006
Posts: 56
Hi,
You are right, I tried the code with different combinations.

The following combination gives error @ runtime in both cases:
JSP code:
<jsp:useBean id="person" type="com.enrolment.Employee" >
<jsp:setProperty name="person" property="name" value="Fred" />
</jsp:useBean>
Name is:<jsp:getProperty name="person" property="name" />

Case 1: Servlet code:
Person p=new Employee();
p.setName("Evan");
req.setAttribute("person",p);

Result: Runtime error[bean person not found within scope ]

case 2: Servlet code
Employee p=new Employee();
p.setName("Evan");
req.setAttribute("person",p);

Result: Runtime error[bean person not found within scope ]

However if you add scope="request" in the useBean action like:
<jsp:useBean id="person" type="com.enrolment.Employee" scope="request">

Then the result is : Name is:Evan in both the cases.

Please correct me if I am wrong.

Thanks and regards,
Saurabh
Rajendra Nath
Greenhorn

Joined: Nov 09, 2005
Posts: 27
hi saurabh...
that's what i needed. our answers are correct.
Kishore Balla
Ranch Hand

Joined: Jun 08, 2005
Posts: 165
Check Errata

http://www.oreilly.com/catalog/headservletsjsp/errata/headservletsjsp.confirmed


SCJP 5.0 : 88% My Story, SCWCD 1.4 : 94% My Story
kishoreballa.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bean related exercise question
 
Similar Threads
Is this an error in HFSJ ??
jsp:usebean
from HF page 356, Be the Container..
Head First Servlets and JSP:SCWCD Question
HFSJ doubt :Pg:356