In my earlier post i had asked abt a mistake from the book HF servlets & JSP(authors basham,kathy,bates).i did not get a reply.
Hope i get a reply from the author atleast for this:
In chapter8 from HF servlets & JSP there is an exercise named "Be the Container" which has the following question..
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" />
what would the above jsp evaluate to if the servlet code looks in the following three different ways?Assume that foo.Person is an abstract class and foo.Employee is a concrete class that extends foo.Person.
1. foo.Person p= new foo.Employee(); p.setName("Evan"); request.setAttribute("person",p);
2. foo.Person p = new foo.Person(); p.setName("Evan"); request.setAttribute("person",p);
3. foo.Employee p = new foo.Employee(); p.setName("Evan"); request.setAttribute("person",p);
Answers given in the book were:
With the first servlet code the request fails at request time since the perrson attribute is stored at request scope,also the jsp:usebean body will not be executed since it specifies only a type attribute.
with the second servlet code it said it fails to compile as foo.Person is abstract.
with the third servlet code it was given like this..The code works fine and prints "Evan".
My question is: Isn't the situation with the first servlet code and third servlet code the same? why has the author given that the third code works fine?where as the first servlet code fails.
Note that the jsp:usebean action does not have a scope attribute which means the default scope-page.but the person attribute is set in the request scope.
can the author or anyone else pls give me an explaination on this question. correct me if im wrong..pls
Cowgirl and Author
Joined: Oct 10, 2002
I *think* this is an error in the book!!
For exactly the reason you mentioned. I didn't write this one, so I have to confirm it, but from what I can see, it looks like the third example would not work for the same reason that the first one won't work--because the standard action does not specify a scope, which means it uses "page" by default, but the servlet code is setting attributes at request scope. So... the result is that no bean will be created, since the standard action specifies *type* but not *class*, and it will fail at request time since there is no attribute under the name "person" in the default page scope.
You're absolutely right about the fact that #1 and #3 have the same problem. Don't know what happened there... but I think this needs to be an official errata.
You're certainly on the right track in your thinking. I wish I could say that we did this on purpose as a learning exercise... but I'm pretty certain it is a mistake that somehow made it past all the reviewers.
Thanks for finding this!! You are going to save others some trouble. -Kathy