File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes bean-related standard actions exercise 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 "bean-related standard actions exercise" Watch "bean-related standard actions exercise" New topic
Author

bean-related standard actions exercise

Bart Allen
Greenhorn

Joined: Dec 19, 2003
Posts: 21
In the Head First Servlets & JSP book, they include the following exercise:
(Chapter 8, page 416)

Look at this standard action:
<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" />

Now imagine that a servlet does some work and then forwards
the request to the JSP that has the code above.
Figure out what the JSP code above would do for each of the
three different servlet code examples.

1.Q) What happens if the servlet code looks like:

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

1.A) Fails at request time! The "person" attribute is stored at request scope, so the <jsp:useBean> tag won't work since it specifies only a type. The Container KNOWS that if you have only a type specified, there MUST be an existing bean attribute of that name and scope.

2.Q) What happens if the servlet code looks like:

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

2.A) Actually, this servlet fails to compile. foo.Person is now abstract, so we can't instantiate the foo.Person

3.Q) What happens if the servlet code looks like:

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

3.A) This works fine and prints out "Evan". Remember, the code INSIDE the body of <jsp:useBean> will never run, since we specified a type without a class.


---->>>

I don't understand why they all wouldn't fail for the same reason the first one failed. There is no attribute in page scope called "person", since it is always being added to the request. The default scope is page, and since we define a type and not a class in all instances, it should fail for the same reason as the first one ...--->>Right???


SCJP, SCWCD, SCBCD, SCJD
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
You are absolutely right about this! This is one of our known errata in the book...
=======================

So, good catch there. You're thinking about it in exactly the right way.

Here's the main errata we have so far (I think there are couple more but I can't get access to the page right now):
========================

1) Question #6 in Chapter 7, the answer should be "D" and not "E".

2) Be the Container in chapter 8, the third example will not work for the same reason that the first example doesn't work--because the attribute is at Request Scope, and the default scope is Page scope.

3) Pg. 114, upside down text should say "Post is considered NOT idempotent".

4) Question 5 in the Filters chapter, the book says the answers are "A" and "G", but "D" should ALSO be marked as a correct answer.


Thanks,
Kathy
Roger Yates
Ranch Hand

Joined: Aug 31, 2004
Posts: 118
If you want to check the rest of them - here's the link to the errata page:
O'Reilly HFS errata page


Cheers,<br />Roger<br />SCJP 1.2, 1.4, SCBCD 1.3, SCWCD 1.4
 
 
subject: bean-related standard actions exercise
 
Similar Threads
Is this an error in HFSJ ??
HFSJ doubt :Pg:356
useBean doubt
Bean related exercise question
jsp:usebean