You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Yang,Chun" <ya...@omrsystems.com> on 2001/02/05 23:58:32 UTC
ClassCastException
Hello all, I am encountering a ClassCastException when compiling a JSP page
in the following scenario:
1) a servlet MyInitServlet creates an object MyObject and saves it to
the context by
getServletContext().setAttribute("myobj", MyObject);
2) a JSP page tries to access MyObject by doing:
MyObject mo = (MyObject) application.getAttribute("myobj");
The above statement caused Jasper to throw a
ClassCastException.
3) I then tried by introspecting the generic object returned by
application.getAttribute()
mo2 = application.getAttribute("myobj");
And I was able to print out the class name and all the public
methods in the class associated with mo2. And everything looked right. But
the statement
mo2 instanceof MyOjbect
still evaluated to false (hence the ClassCastException)......
What did I do wrong ?
Your help is greatly appreciated!
Re: ClassCastException
Posted by Shahed Ali <sh...@enoor.com>.
Are you using an inner class for declaring MyObject ?
I faced a similar problem when I daclared a class as below in a jsp page,
create an object of type ax,
saved it in a session, and retrived it in another jsp page which had the
same <%! class ax ...%>
code fragment.
<%!
class ax
{
public int x;
}
%>
The problem is that you cannot cast inner classes as they are qualified by
their enclosing class (AFAIK)
Try moving class MyObject to a seprate .java file.
Regards
Shahed