• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

bean-related standard actions exercise

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to check the rest of them - here's the link to the errata page:
O'Reilly HFS errata page
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic