• Post Reply Bookmark Topic Watch Topic
  • New Topic

type and class attributes in jsp:useBean

 
John Russell
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was under the impression that, if you use both a type attribute and a class attribute, and the jsp:useBean constructs an object, then the object is constructed using the class specified in the class attribute, but the reference to the object is supposed to be of the type specified in the type attribute. In practice, with Tomcat 5.5, it appears the reference is also of the type specified in the class attribute.

For example, I have an Employee class with an empID property, which extends a Person class that has no such property. When I execute the following JSP code with no pre-existing request attribute named "person", it constructs an Employee object -- that's fine -- but then I can access all the special Employee properties through the reference I get...it doesn't behave like a Person reference at all.



This is displayed:


Why I can access the empID property? Is it supposed to work this way?

Thank you for any thoughts you may have on this.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the best things you can do for yourself to understand how things like this work is to find the Java class that Tomcat creates on behlaf of your JSP and see what code it generates. You'll find such files in the folder hierachy under Tomcat's work folder.

In this case, you'd find that, as you expected, the scripting variable person is created as a reference to Person and using the Employee constructor. However, your expectation of how the getProperty actions are applied is incorrect.

You are probably assuming that an action such as:



gets turned into code like:



If that were the case, then yes, you would expect a compilation fault as the person variable of type Person would not have access to the property defined on the Employee class.

However, the setProperty action does not get translated into direct code such as that, but rather is converted into a call into a Tomcat-specific method that uses Java reflection to access the bean. As such, the bean is treated as whatever object it actually is irrespective of how the scripting variable is declared.

In case you were curious, the gnerated code is:


[ June 21, 2006: Message edited by: Bear Bibeault ]
 
John Russell
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response! It's actually the jsp:getProperty behavior that has me puzzled. When I look at the code generated for this...



...I find this:



What puzzles me is that the attribute is being cast to com.foo.Employee, not com.foo.Person. In other words, it's using the type specified in the *class* attribute of the jsp:useBean tag, not the type specified in the *type* attribute. What is the point of specifying the type attribute at all? What is the point of creating a Person reference to the Employee object if we are going to cast the Person reference back to Employee whenever we use it? Right now, I don't see any practical difference between a jsp:useBean element that has a type attribute and one that doesn't. I think I must be missing something, because surely there must have been a purpose in mind for the type attribute.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't find it puzzling. You want to cast to the child class to make sure to get all attributes, not to an interface which does not contain any attributes anyway.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the point of specifying the type attribute at all?


The difference comes when referencing the variable in Java scriptlet code. As you have seen, it has no effect on the property actions.
 
John Russell
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference comes when referencing the variable in Java scriptlet code. As you have seen, it has no effect on the property actions.


Bear, you are the man! I don't understand why they made it work that way,javascript: x() but you've helped me understand how it works.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to be of help!

And now that you understand it, it is all becoming moot as the JSTL and EL eliminate the need for Java scriplets and the property actions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!