Forums Register Login

Question from HF second edition

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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....
+Pie Number of slices to send: Send
Thanks for the quick reply. The book has given like below with the scope:

<jsp:useBean id=�person� type=�foo.Employee� scope=�request� >
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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:

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

Rufus.
+Pie Number of slices to send: Send
Thanks guys
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 907 times.
Similar Threads
does this jsp code will work?
doubt in jsp:usebean
Is this an error in HFSJ ??
HFSJ 2nd edition page 420 - Be the Container Answers
useBean doubt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:40:31.