Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question from HF second edition

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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� >
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic