Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from HF second edition

 
Tyronne Fernando
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am not sure whether this is posted here or not. The following question is from Head First Servlets and JSP second edition page 358.

----------------------------------------------------------------------

Look at this standard action:

<jsp:useBean id=�person� type=�foo.Employee� scope=�request� >
<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 the
following servlet code example.

What happens if the servlet code looks like:

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

----------------------------------------------------------------------

The answer given in the example is:

----------------------------------------------------------------------

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.

----------------------------------------------------------------------

I thought it will print "Name is: Evan". Is the answer is not correct in the book? I am not sure why it will fail as there will be a request attribute. Can someone please explain? Thanks heaps.
 
Sarat Koduri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fernando.. would you please confirm.. is this the code they have given..


i gues.. they have given.. like this..

because in this case as you might know the bean will be looking for page scope only....
 
Tyronne Fernando
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply. The book has given like below with the scope:

<jsp:useBean id=�person� type=�foo.Employee� scope=�request� >
 
John Pearson
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't see any problem with the code as you listed it, so I wrote a quick test app just to see what it did. It worked just fine! Maybe it is a mistake in the wording of the question.
 
Rufus Addis
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has cropped up a few times. IMHO it's an error in the 2nd edition and is mentioned in the unconfirmed section of the Errata. See the following thread:

http://www.coderanch.com/t/178060/java-Web-Component-SCWCD/certification/HFSJ-Container-page

Rufus.
 
Tyronne Fernando
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic