*
The moose likes JSP and the fly likes a big doubt on jsp:useBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "a big doubt on jsp:useBean" Watch "a big doubt on jsp:useBean" New topic
Author

a big doubt on jsp:useBean

Sayak Banerjee
Ranch Hand

Joined: Nov 28, 2006
Posts: 292
Hi guys....could you please help me out with this....it's not mentioned in the errata list for HFSJ....The 12th bullet on Page 414 point says --If you specify a "type" attribute in <jsp:useBean>, you can set properties in <jsp:setProperty> ONLY on properties of the "type", but NOT on properties that exist only in the actual "class" type.(In other words, polymorphism and normal Java type rules apply.)
I guess what they meant was THIS :

IF the classes are :
package foo;
public abstract class Person { private String name;
public void setName(String name) { this.name=name; }
public String getName() {return name;}
}
public class Employee extends Person{ private int empID;
public void setEmpID(int empID){ this.empID=empID; }
public int getEmpID() {return empID;}
}

THEN assuming the bean "testbean" doesn't exist is any of the scopes:
using
<jsp:useBean id="testbean" type="foo.Person" class="foo.Employee" >
<jsp:setProperty name="testbean" property="empID" value="656" />
</jsp:useBean>
would produce an error ....I think that's what they meant and it is justified as well because in Java you cannot do this ...
eg. foo.Person p=new Employee(); p.setEmpID(656)......


BUT...I using Tomcat 5.0.28 and, tell you what, it works fine when the above useBean code is used ....I'm dazed...please help
[ November 29, 2006: Message edited by: Sayak Banerjee ]

Turn on, tune in, drop out.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

In useBean, try to remove the "class" attribute. If you specify the class attribute, the container will know that the bean is an Employee.


[My Blog]
All roads lead to JavaRanch
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Sayak Banerjee:
...--If you specify a "type" attribute in <jsp:useBean>, you can set properties in <jsp:setProperty> ONLY on properties of the "type", but NOT on properties that exist only in the actual "class" type.(In other words, polymorphism and normal Java type rules apply....)


The servlet spec differs from this.

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


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

Joined: Nov 28, 2006
Posts: 292
I see....so does that mean the container does somethin' of this sort internally :
foo.Person p=new Employee();
((foo.Employee)p).setEmpID(656);

??
[ November 29, 2006: Message edited by: Sayak Banerjee ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Most containers give you an option to retain and view the generated servlet code. Tomcat, for instance holds the Servlet source files under tomcat/work/Catalina/localhost/YOURAPP_NAME/org/apache/jsp.
You can take a look and see exactly what it's doing.

I know Tomcat has some of it's own 'introspectionHelper' classes for determining what properties a bean has before trying to write to them.
Sayak Banerjee
Ranch Hand

Joined: Nov 28, 2006
Posts: 292
Thanks for the response Ben....This is what I found out....
The container is using the function below for the purpose of setting the property value which bends the polymorphism rules and I'm not sure how exactly this works :-

org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("testbean"), "empID", "656", null, null, false);

I got it from the generated servlet code...Now that we know this.....

does that call for a correction of the 12th bullet point on page 414 in the HFSJ book?
 
Consider Paul's rocket mass heater.
 
subject: a big doubt on jsp:useBean