jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp:usebean" Watch "jsp:usebean" New topic


A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 980

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:getProperty name="Emp" property="name"/>
<jsp:getProperty name="Emp" property="empId"/>

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

Ben Souther

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: 980
So is it like ....the bean ojbect & its properties is considered ...at the time of setting them...and not the reference...

[ September 29, 2006: Message edited by: A Kumar ]
I agree. Here's the link: http://aspose.com/file-tools
subject: jsp:usebean
It's not a secret anymore!