jQuery in Action, 3rd edition
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "bean-related standard actions exercise" Watch "bean-related standard actions exercise" New topic

bean-related standard actions exercise

Bart Allen

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" />
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();
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();
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();
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???

Kathy Sierra
Cowgirl and Author

Joined: Oct 10, 2002
Posts: 1589
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.

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
I agree. Here's the link: http://aspose.com/file-tools
subject: bean-related standard actions exercise
It's not a secret anymore!