• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confusion about value attribute expression in <jsp:setProperty>

 
chandan kumar mitwaa
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have:


1.a ) A Dog class as:



1.b ) The Person class is:




2) A SimpleServlet class as:





3) A index.jsp file containing:



4) A Print.jsp file containing:




5) And the JSP specification for <jsp:setProperty> mentions:

When assigning from a value given as a request-time attribute, no type
conversions are applied if a scripting expression is used
, as indicated in
Section JSP.1.14.2.2, “Conversions from request-time expressions”. If an EL
expression is used, the type conversions described in Section 1.16 “Type
Conversion” of the EL specification document are performed.



When I press Go on index.jsp:

The ouput is:

Breed: Labrador Price: Rs.10000
Breed: Cocker Spaniel Price: Rs.15000




According to me Print.jsp should produce request time exception.
Am i right or have misunderstood the specification statement.
Please reply.

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
application.getAttribute("specialbreed") returns an array of dogs, doesn't it ? Where do you see a conversion happening ?
 
chandan kumar mitwaa
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:application.getAttribute("specialbreed") returns an array of dogs, doesn't it ? Where do you see a conversion happening ?


The implicit object 'application' is of type ServletContext:
and from api for ServletContext we have:
java.lang.Object getAttribute(java.lang.String name)
Returns the servlet container attribute with the given name, or null if there is no attribute by that name.


The return is of type Object. Isn't there a conversion from Object ti Dog[]???
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not know why it should throw exception?

According to your code the <jsp:setProperty> will execute only if the bean id man does not available in the jsp.

But your page has bean id man from the Servlet.

So there will be no exception.

Correct me if i am wrong.
 
Kapil Mishra
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conversion happens only for String and Primitives.....You have Object returned from getAttribute() function but implicitly it is object of Dog[],
So i don't think there would be a problem...

 
Chinmaya Chowdary
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Chandan.

While executing container get's the 'specialbreed' attribute, since it returns of type 'Object' I think it internally uses casting like, and sets the Dog array object to java bean property.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<jsp:setProperty name="man" property="dogs" value="<%=application.getAttribute(\"specialbreed\")%>" />


As per his code the above line will never execute at runtime.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic