aspose file tools*
The moose likes JSP and the fly likes Using JavaBeans in JSP pages- ClassCastException problem  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Using JavaBeans in JSP pages- ClassCastException problem  " Watch "Using JavaBeans in JSP pages- ClassCastException problem  " New topic
Author

Using JavaBeans in JSP pages- ClassCastException problem

Reema Patel
Ranch Hand

Joined: Jan 26, 2006
Posts: 169
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

Joined: Jan 10, 2002
Posts: 60071
    
  65

It does not match by type, only by name.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Reema Patel
Ranch Hand

Joined: Jan 26, 2006
Posts: 169
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using JavaBeans in JSP pages- ClassCastException problem
 
Similar Threads
does the jsp engine look for the declared/actual type before instantiating javabean ?
Bean instantiation from JSP
jsp:useBean question
Problem when the struts validator rejects user input
what/how to do (if above contains shopping cart items of type OrderPlantItem and total vars), set it