my dog learned polymorphism*
The moose likes JSP and the fly likes jsp:usebean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp:usebean" Watch "jsp:usebean" New topic
Author

jsp:usebean

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

I have a form with 2 input fields and i submit the form to a jsp which has this code

<jsp:useBean id="Emp" type="com.Person" class="com.Employee">
<jsp:setProperty name="Emp" property="name"/>
<jsp:setProperty name="Emp" property="empId"/>
</jsp:useBean>
<jsp:getProperty name="Emp" property="name"/>
<jsp:getProperty name="Emp" property="empId"/>


Here,
com.Person is abstract with a field name

com.Employee is concrete extending Person and has field empId

Now i am able to get the values of both the fields printed..

But my doubt is....
since Person is the reference type ..how is it able to set the value of empId that belongs to Employee..

the servlet code will be something like this...

Person Emp=new Employee();

Then how is it able to access the empId ...variable

Regards
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

That's a good question.
I just tried this in Tomcat, and the answer is that it uses introspection to find the properties.


The generated servlet source....


And the reason introspection is used...

JSP.5.2 <jsp:setProperty>
The jsp:setProperty action sets the values of properties in a bean.
The name attribute that denotes the bean must be defined before this
action appears. There are two variants of the jsp:setProperty action.
Both variants set the values of one or more properties in the bean
based on the type of the properties. The usual bean introspection
is done to discover what properties are present,
and, for each, its
name, whether it is simple or indexed, its type, and the setter and
getter methods. Introspection also indicates if a given property type
has a PropertyEditor class.

[ September 29, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
So is it like ....the bean ojbect & its properties is considered ...at the time of setting them...and not the reference...

Regards
[ September 29, 2006: Message edited by: A Kumar ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp:usebean
 
Similar Threads
EL and jsp:usebean..type/class
jsp:useBean
<jsp:useBean>
Polymorphism and useBean
page 360 if property="*"