Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Struts/jsp Class Cast Exception

Reply
Thread Tools

Struts/jsp Class Cast Exception

 
 
david
Guest
Posts: n/a
 
      08-22-2003
I'm in the midst of developing a small webapp, I'm a beginner. And,
I've run into an error I don't understand.

I'm posting a session attribute in an Action...
curSession.setAttribute("testorg", O); where O is a vector containing
orgVO class objects.

Here is my jsp...
---------------------------------------------------------------------------
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="Beans.*, java.util.*" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<%
orgVO[] newOrg = new orgVO[1];
newOrg[0] = new orgVO("david", "test");
out.println(newOrg[0].getorgName());


HttpSession catalogSession = request.getSession(true);
Vector test = (Vector) catalogSession.getAttribute("testorg");
Object [] orgtest = test.toArray();
Object d = orgtest[0];
Class x = d.getClass();
String classX = x.getName();
out.println("**" + classX + "**");
//orgVO testd = (orgVO) d;

if (test == null) {out.println("test data not received");} else {
int i = test.size();
out.println("size = " + i);
for (int x1 = 0; x1 < i; x1++){
// orgVO t = (orgVO) test.elementAt(x);
// out.println(t.getorgName());
// out.println(test.elementAt(x).getorgName());
}
out.println("test data received");}
%>


//<logic:iterate id="element" collection="<%=test %>"
type="Beans.orgVO">
//<bean:write name="element" property="orgName" filter="true"/>
//</logic:iterate>


<html:html locale="true">
<head>
<title><bean:message key="index.title" /></title>
<html:base/>
</head>

</html:html>
---------------------------------------------------------------

when I run the jsp with the above as is (with the casting commented
out) it works. I see that there are 83 objects in the vector, and
that they are of class orgVO. Additionally, the initial section
instantiates an orgVO class and I'm able to print out the name.
However, If i uncomment the code I get a class cast exception for
orgVO - i get this same exception with the using the iterate bean and
with the java code.

error:

org.apache.jasper.JasperException: orgVO
at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:254)
at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:241)...

root cause

java.lang.ClassCastException: orgVO
at org.apache.jsp.catalog_jsp._jspService(catalog_jsp .java:116)
at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:137)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)...


Any one have any ideas/suggestions?

thanks,
david
 
Reply With Quote
 
 
 
 
Dave Glasser
Guest
Posts: n/a
 
      08-23-2003
(david) wrote on 22 Aug 2003 06:37:38 -0700 in
comp.lang.java.programmer:

>I'm in the midst of developing a small webapp, I'm a beginner. And,
>I've run into an error I don't understand.
>
>I'm posting a session attribute in an Action...
>curSession.setAttribute("testorg", O); where O is a vector containing
>orgVO class objects.
>
>Here is my jsp...


(snip)

>when I run the jsp with the above as is (with the casting commented
>out) it works. I see that there are 83 objects in the vector, and
>that they are of class orgVO. Additionally, the initial section
>instantiates an orgVO class and I'm able to print out the name.
>However, If i uncomment the code I get a class cast exception for
>orgVO - i get this same exception with the using the iterate bean and
>with the java code.
>
>error:
>
>org.apache.jasper.JasperException: orgVO
> at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:254)
> at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:295)
> at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:241)...
>
>root cause
>
>java.lang.ClassCastException: orgVO
> at org.apache.jsp.catalog_jsp._jspService(catalog_jsp .java:116)
> at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:137)
> at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)...
>
>
>Any one have any ideas/suggestions?



It probably has to do with classloaders. The objects in your vector
are of the orgV0 class, but their class was loaded by a different
classloader than the JSP's, and it was not a direct ancestor of the
JSP's classloader. Try making absolutely sure that the orgV0 class is
not in the system classpath when Tomcat starts, or in a jar file in
Tomcat's lib directory. Only let it be beneath the WEB-INF/classes
directory for your web app.

If that doesn't work, try putting the orgV0 class *in* your system
classpath and see if that helps. If Tomcat is following the rules for
classloaders, then the orgV0 class should only be loaded by the system
classloader, however that might not be the case.



----
Check out QueryForm, a free, open source, Java/Swing-based
front end for relational databases.

http://qform.sourceforge.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class cast exception matrixman333 Java 1 05-14-2006 09:02 PM
Class Cast Exception and I can't figure it out da_rod_father Java 3 02-12-2006 05:51 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
Class cast exception Will Java 2 10-18-2004 07:17 PM
Invalid Cast Exception Calling Bsse Class Method Nick Flandry ASP .Net 5 02-12-2004 04:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57