• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One doubt in " HF servlets and jsp" book.

 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Ranchers,
I have one doubt while reading HFServlet&Jsp on question in page no 356.
the question goeslike this

<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"/>

and the servlet code are ...

option 1:
foo.Person p=new foo.Employee();
p.setName("Evan");
request.setAttribute("person",p);

option 2:
foo.Person p=new foo.Person();
p.setName("Evan");
request.setAttribute("person",p);

option 3:
foo.Employee p=new foo.Employee();
p.setName("Evan");
request.setAttribute("person",p);

where Person is an abstract class with seter and geter of property "name" and Employee is a concrete subclass of Person having setter and getter for property "empID" and both are in package "foo".

For me option 1 and 2 is correct as in
option-1 we are setting attribute in request scope but in jsp page we are not specifying any scope so by default it will serch in "page" scope and it won't find it and on request it will fail.
option-2 is also corrext as we are tying to cereate one instance of abstract class.
but my doubt is in option-3 , in this case also we are setting attribute in request scope and trying to find it in page scope in jsp page which should give same error as in option-1 but the anser is given that it is correct and it will print the name.
how it is happening ? will anyone help me out .
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats an errata
the note that holds as explanation for option 1, should hold for option 3 also
 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Niranjan for your quick reply.
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think every options are incorrect.

if <jsp:useBean> uses only type attribute then it will try to locate any existing object in the scope. If not, its will not create any new object.

In every options you are using new operator. So i suppose its wrong!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic