• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:usebean type attribute

 
gurneeraj singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SERVLET:


Above servlet has set attribute "beer" in request scope then forward request to view jsp(shown below)

view.jsp:



Where Beer class extends Alcohol class




Ques: Why the above code is showing error when alcohol attribute already exist in request scope? please explain.

[ August 22, 2008: Message edited by: gurneeraj singh ]
[ August 22, 2008: Message edited by: gurneeraj singh ]
 
deepa raj
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your alcohol atrribute refer to the Beer Object originally but the reference object is Alcohol.

And again you are trying to map the Alhocol Object into the Beer Reference Object using useBean action (in compile time.)That is reason for the error.

If you change the type as Alcohol , it will work fine.
 
Jan Sterk
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't agree. I think it should work. If you cast an object, it is the actual class that counts, and not the type of the variable.

I tried this:


It works fine, it doesn't throw a ClassCastException.

Gurneeraj, what is exactly the error message?
 
Krzysztof Koziol
Ranch Hand
Posts: 133
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my understanding it should be:

 
deepa raj
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the above code is working fine.
 
Jan Sterk
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see now that I used 'class=..' instead of 'type=..'. But with type-only it also works fine, since the attribute already exists.

 
gurneeraj singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this code should work as attribute already exist in request scope but then why on page number 358 of HFSJ2, answer to first question(which is similar to above coe) is an error?

Please reply!
 
Milind Patil
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gurneeraj,

Its working fine. Check tag syntax. Tag names and attributes are case sensitive.


<jsp:useBean id="alcohol" type="com.test.Beer" scope="request"/>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic