Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Doubt-page356

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<jsp:useBean id="person" type="foo.Employee">
<jsp:setProperty name="person" property="name" value"Fred"/>
</jsp:useBean>


servlet code:

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


the answer given for this "Be the Container" is -it wont work because container looks for bean in page scope while actual bean exists in request scope.



Ok..now my doubt...
lets make the scope as request instead of default page scope
<jsp:useBean id="person" type="foo.Employee" scope="request">

will this work??
i mean i'm not sure how container generates the code when only type is given..

foo.Employee person=null;
synchronized(request){
person=(???downcastedto???)pageContext.getAttribute("person",PageContext.REQUST_SCOPE)
....
}

person will be downcasted to foo.Employee or foo.Person??if it is foo.Person then u get an exception since u cant assign foo.Person object to foo.Employee reference..
i'm confused
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandya,

If only the type is given, then the container will *not* create a new bean. The setProperty inside the useBean tag will be ignored. In this case, however, if you added scope="request" to the useBean statement, there would be a bean so setProperty would be invoked.
Since the Employee is a subclass of Person, the a reference to a person can happily refer to an Employee as per standard java rules.

Since you've told the container to use foo.Person as the type, you would get something like:
foo.Person person=null;
synchronized(request){
person=pageContext.getAttribute("person",PageContext.REQUST_SCOPE)
....
}

So the Employee object (which IS-A person) you create in the servlet will happily be referred to with a Person reference.

HTH
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger

I disagree with the below statement made by you.

"however, if you added scope="request" to the useBean statement, there would be a bean so setProperty would be invoked."

As per my understanding setProperty runs only if the bean is not found

correct me if I am wrong

Thanks
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic