• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

useBean, getProperty and typecasts

 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to everybody,
I have a question that was born from the message MSG:

Let's consider the following scenario:
1. There's a Person bean with 'name' property
2. There's a subclass of Person, say Employee
3. There's a servlet that set 'p' request attribute with an instance of 'Person' class and dispatches the request to the test.jsp page
4. There's a jps page called test.jsp that contains this portion of code:
<jsp:useBean id="p" type="Person" class="Employee"/>
<jsp:getProperty name="p" property="name"/>

When using <jsp:getProperty>, the application server translates <jsp:getProperty> with something like

((Employee) pageContext.getAttribute("p",PageContext.REQUEST_SCOPE)).getName(),

which throws a ClassCastException.
The problem does not occur if any of this points occurs:
1. Replace <jsp:getProperty> with EL or script
2. Replace the 'class' attribute with 'beanName' attribute

I've looked for some more details about this behaviour of <jsp:getProperty>, but from the specs I could not find a reason why it should use 'class' attribute, instead of 'type'.

Thanks in advance
 
Stary Kapec
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When using <jsp:getProperty>, the application server translates <jsp:getProperty> with something like

((Employee) pageContext.getAttribute("p",PageContext.REQUEST_SCOPE)).getName()


Hi
Not exactly, findAttribute method is used instead

((Employee) pageContext.findAttribute("p")).getName()
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mirko,

The reason why "class attrubute" considered for casting the Object returned by findAttribute method is

1.Bec'ze Object returned may contain properties of Employee also.
If it uses "type attribute" for casting the object How can you
access Employee specific properties.

thats the advantege we get by specifyting the class.

2.With EL you are able to get the result.
Why you got:
2.1) useBean action translated to


If the Object assosiated with name "p" found then
Person p = (Person)FoundObject;
else
Person p = new Employee();

Due to the use of "type attribute" its storing it in Person ref.
Hence it works well ${p.name} thats why it works.
So it may produce name or display nothing(bec'ze EL supresses null display).
[ May 17, 2007: Message edited by: Srinivasan thoyyeti ]
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
thanks for your response.

However, if it happens 'Bec'ze Object returned may contain properties of Employee also', this means that the typecasting mechanism +class attribute cannot be used with <jsp:getProperty> standard action; we can find any explainations about this behaviour, but nothing on the specs refers to it, or at least I could not find anything about .

Moreover, if I specify a <type> different from <class>, maybe I have considered the fact that my bean might be of a different class, but surely of type <type>: so I should not refer to a property that does not exist in <type>.

Thanks
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mirko,

getProperty inturn depending on useBean which is very flexible.
Hence whenever it takes inputs from useBean those will also vary according to what is present in useBean.

Spec of getProperty: The object named by the name must have been �introduced� to the JSP processor using either the jsp:useBean action or a custom action with an associated VariableInfo entry for this name. If the object was not introduced in this manner, the container implementation is recommended (but not required)to raise a translation error, since the page implementation is in violation of the specification.


Spec of useBean:The jsp:useBean action is quite flexible; its exact semantics depends on the attributes given. The basic semantic tries to find an existing object using id and scope. If the object is not found it will attempt to create the object using the other attributes.


What you say.
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am not sure if the explanation that I am going to write is satisfactory or not but this is what I have to say:
As per JSP.5.3 "The value of the name attribute in jsp:setProperty and jsp:getProperty will refer to an object that is obtained from the pageContext object through its findAttribute method." Now as we all know findAttribute method return object of type 'Object' class which should be typecasted. Now we want to know should it be typecasted to the value of type or class attribute of useBean action, as per specs.
About type what I understand from specs is, "Type if specified, it defines the type of the scripting variable defined. This allows the type of the scripting variable to be distinct from, but related to, the type of the implementation class specified." So when useBean action is used to create the object it uses the value of class or beanName attribute to create the object. If none are specified it will result in InstantiationException. Why should you typecast an object on the type of its reference variable and not on its object type. It is very clear from specs that type attribute is only for the type of scripting variable which is used to point at the object and it is not used to create the object, this is the reason Object returned by findAttribute method should be typecasted to type of the object and not the type of its reference variable.
I am not sure if I am making sense here or not. But this is what I have understood uptill now.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rancy,

from my previous post...

its clear that getProperry should rely on useBean attributes to decide which attribute it has to select for casting the Object.

1. if useBean cantains only "type attribute" then getProperty will only
use that "type attribute" for casting the object found.

2. if useBean contains both "type attribute" and "class attribute" then
getProperty has to use "class attribute" for casting the object found.

Hence it depends on what is available in useBean.
let me know any conflicts.
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srinivasan,
thank you for your response.

Yes, it's the point! But I can't find this behaviour into the specs.
I mean, if one day I decide to write my own application server, I might use '<type>' cast instead '<class>' cast when using jsp:getProperty without violating jsp 2.0 specs.
 
Stary Kapec
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what Rancy Chadha wrote:

Type if specified, it defines the type of the scripting variable defined.

It is very clear from specs that type attribute is only for the type of scripting variable which is used to point at the object


What is the reason for the scripting variable? Both jsp:getProperty and jsp:setProperty neither use it but obtain the reference from pageContext.findAttribute invocation.
So why is the new reference (scripting variable) created at all?
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jasiek,
You mentioned:
Both jsp:getProperty and jsp:setProperty neither use it but obtain the reference from pageContext.findAttribute invocation.
So why is the new reference (scripting variable) created at all?

Here scripting variable is nothing but the variable declared by the id attribute of useBean action. And ofcourse getProperty and setProperty actions use it. In findAttribute method, value of 'id' is used as the parameter to it. This explains why scripting variable is created. Not only can we use this variable name in getProperty and setProperty but we use it in expressions as well as scriptlets in the JSP page.
 
Stary Kapec
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rancy


Here scripting variable is nothing but the variable declared by the id attribute of useBean action.


Yes I agree and the name of the variable is as the value of an id attribute.

And ofcourse getProperty and setProperty actions use it.


Here I disagree.

In findAttribute method, value of 'id' is used as the parameter to it. This explains why scripting variable is created.


Yes, the value of an id attribute is used, but only the value. The created reference variable plays no role here.


Not only can we use this variable name in getProperty and setProperty but we use it in expressions as well as scriptlets in the JSP page.


Yes, in scriptles the variable can be used. But actions were introduced to avoid using scriptlets. So it is not a real advantage.

In EL expressions still the variable is not used.

${p.name}

In the above example p element points to an attribute not any scripting variable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic