• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using JavaBeans in JSP pages- ClassCastException problem

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic