This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt-page356

 
Sandya Bhaskara
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
 
Roger Yates
Ranch Hand
Posts: 118
  • 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
 
Luke
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
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!