Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using JavaBeans in JSP pages- ClassCastException problem

 
Reema Patel
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
To begin with, I have a simple inheritance hierarchy:
Class B and class C, both extend from class A

Next, I have the following two JSP pages:

infobean1.jsp
<jsp:useBean id="info" class="com.abc.B" scope="session" />
<jsp:forward page="/infobean2.jsp" />

infobean2.jsp
<jsp:useBean id="info" class="com.abc.C" scope="session" />

I get a ClassCastException (reason being that B and C both are different types, although they have a common base class). But, my query is on how the <jsp:useBean> element behaves (pls. correct me wrong)
infobean1.jsp
<jsp:useBean id="info" class="com.abc.B" scope="session" />
The JSP engine tries to look for a bean type B in the session scope with id- info, it fails and instantiates a new object, and then we have a forward...

In infobean2.jsp page [same session]
<jsp:useBean id="info" class="com.abc.C" scope="session" />
The JSP engine tries to look for a bean type C in the session scope with id- info, it suceeds at compile time but fails at run-time nd hence we get the ugly strack trace, but my question is that why does it suceeds at the first place, cause B and C are both un-related classes, rather it should fails and instantiates a new object.....(and then ofcourse we should not get the exception trace)

Pls. help...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does not match by type, only by name.
 
Reema Patel
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear..
I think the correct answer should be "it finds a match with the name and within the specified scope"..

Something like this
<name-id> = (<type>) _jspx_page_context.getAttribute("<name-id>", PageContext.PAGE_SCOPE);
....
.....
...

Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic