• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a big doubt on jsp:useBean

 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In useBean, try to remove the "class" attribute. If you specify the class attribute, the container will know that the bean is an Employee.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic