• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use of type in <jsp:usebean>

 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to understand the use of type and class in the jsp standard action <jsp:usebean> . I have 2 questions regarding this.
1. In HFSJ Ch 8 pg 356 (we have abstract class Person and concrete class Employee) it says that
“If type is used without class, the bean must already exist”
but then in the example given:
<jsp:usebean id=”person” type=”foo.Person” scope=”page”/>
it works if the person attribute already exists in “page” scope otherwise you get an InstantiationException.
So I am assuming the authors are saying that if Person were a concrete class using type without class will work if person attribute is in page scope otherwise you will get the exception. Does that mean that if Person were a concrete class and the attribute was not in page scope you would get an error? Can anyone please clarify this for me?
2. My second question concerns the quiz “Be the Container” on pg 358.
Here we have:
<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”/>

The servlet code is:
foo.Person p = new foo.Employee();
p.setName(“Evan”);
request.setAttribute(“person”, p);

We know that the body of the tag will never run if the bean already exists. If the bean already exists then using type=”foo.Employee” should work. Why does it not work here? Is it because the Container knows that Employee is a subclass of Person? Would it have worked if Employee were not a subclass of Person and the servlet code said
foo.Employee e = new foo.Employee();
p.setName(“Evan”);
request.setAttribute(“person”, p);

I appreciate your input in advance.
Thanks,
Meera.



 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean that if Person were a concrete class and the attribute was not in page scope you would get an error?

Yes, whatever "person" is, if it's not in page scope, an error will occur.


Why does it not work here?

Does it explain why it doesn't work ? I think it should work.
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes on page 420 in the answer section the explanation given is it "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 am confused with this explanation because it seems to be talking about 2 different things as to why it fails:
1) scope: request, page
2) use of type.
Here the bean Employee is already existing. So that must not be the reason why it failed. So it fails because the scope is request?
My take on it was that Person is abstract and Employee is concrete and when type is used without class where there is an inheritance it fails.
Please clarify.
Thanks,
Meera
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "person" attribute is stored at request scope, so the <jsp:usebean> tag won't work since it specifies only a type.

Wasn't the question looking like :

with no scope specified ?
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Meera,
Check the errata of HFSJ.
page 420
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody. It makes sense now.
Meera
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic