Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Class cast exception  RSS feed

 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Im using tomcat server. but whenver i make some changes in my classes i need to restart the machine or else its giving me ClasscastException. i need to know the reason and how this can be avoided.
And also where to put the properties file and .tld files in tomcat. do we need to give the location of these in web.xml ?
Thanks in advance
Cherry
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it has anything to do with a similar problem in Weblogic, it's possibly a java.lang.ClassLoader problem.
As I understand it, App servers can have multiple ClassLoaders for loading classes. If a class is loaded in multiple ClassLoaders and they get compared it is NOT considered the same class.
The question now is 'Why am I getting classes being compared from different classloaders...'
Again my understanding (as it applied to BEA-WL): you can dynamically unload a class by killing the ClassLoader that loaded it. Therefore if you want to (for instance) recompile and reload a JSP that has changed you need to kill the ClassLoader it came in. If any instances of classes loaded by this ClassLoader are allowed to live, they won't be the same as the ones created by the new ClassLoader.
Additionally, if you create an object in a Servlet and pass it to a JSP (via the session or request) and the JSP has a different ClassLoader then you'll get a ClassCastException...
To get around it there's a cool little trick you can use by serializing the object, passing it from the object accessing it back to itself via an ObjectStream (and loading it in the new ClassLoader in the process) then casting it to the correct type.
Good luck
Dave.
 
Adam Chace
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as the .tld file goes in Tomcat, you can put this right in the root of the WEB-INF directory of your web application, or you can specify another location for it in the web.xml - something like WEB-INF/tlds.
-Adam
 
zheng yu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can David show me some sample code to demo your little cool trick .
I think i am now running into such problems , hope you can help me out

Regards
ZY
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haven't tested it in a while (had to rewrite) but it looks a little like this:

Can you get back to us if it works so we can all share?

Dave
 
zheng yu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David
Your Trick worked . i am so happy ( because i have stuck up for 1,2 days 8-D )
My original design is :
Servlet :
.Execute specified SQL statement to get a resultset
.stuff all the tuples in that resultset into a LinkedList variable
.store this LinkedList object into HttpSession
JSP :
.get LinkedList variable from session
.traverse all the objects in that linkedlist and display its contents in client browser
My self-defined record structure is like this :
package search.common;
public class strucZassiKangouItem implements ijava.io.Serializable {
public StringstrKangou;public StringstrKiji;public StringstrTitleCode;
public strucZassiKangouItem() {
strKangou= null ;
strKiji= null;
strTitleCode= null;
}
}//:-
My servlet program is like this :
...
objBean.execSql() ;
// Set Session Variable
session.setAttribute( Commondef.SEARCHFLAG , strSearchFlag) ;
session.setAttribute( Commondef.SEARCHSTRING, strSearchString) ;
session.setAttribute( Commondef.CURRENTPAGE, String.valueOf(objBean.getPageCurrent())) ;
session.setAttribute( Commondef.OPERATION, strOperation) ;
session.setAttribute( Commondef.TITLECODE, strTitleCode) ;
session.setAttribute( Commondef.DISPLAY, objBean.getByteArray()) ;
....
getByteArray Function is like ( note Query result has been stored in lstResult ):
public byte[]getByteArray() {
byte[]bArray = null ;
try {
ByteArrayOutputStreambaos= new ByteArrayOutputStream();
ObjectOutputStreamoOut= new ObjectOutputStream( baos ) ;
oOut.writeObject( new Integer( lstResult.size()) ) ;
for ( int iLoop = 0 ; iLoop < lstResult.size() ; iLoop ++ ) {
oOut.writeObject( lstResult.get( iLoop )) ;
}//end for

oOut.flush();
bArray= baos.toByteArray() ;
oOut.close() ;
}catch ( IOException e ) {
baseServlet.Log("IoException occured in getOutputStream()") ;
}
return bArray ;
}
My JSP is like :
...
byte[]bArray= (byte[])session.getAttribute(Commondef.DISPLAY ) ;
ByteArrayInputStream bais= new ByteArrayInputStream( bArray ) ;
ObjectInputStream oIn= new ObjectInputStream( bais );
...
<TABLE width="896" border="0" cellpadding="0" cellspacing="0">
<%
int iLoop= 0;
for ( iLoop = 0 ; iLoop < objCount.intValue(); iLoop ++)
{
strucZassiKangouItemobjItem= ( strucZassiKangouItem )(oIn.readObject()) ;
String strKangou= "";
String strKiji= "";
String strTitle= "";
if ( objItem.strKangou != null )
strKangou= new String ( objItem.strKangou.getBytes() , "JISAutoDetect");
else
strKangou= "";
if ( objItem.strKiji != null )
strKiji= new String ( objItem.strKiji.getBytes() , "JISAutoDetect");
else
strKiji= "";
if ( objItem.strTitleCode != null )
strTitle= new String ( objItem.strTitleCode.getBytes() , "JISAutoDetect");
else
strTitle= "";
%>
<TR>
<TD width="490" height="88"><B><%=strKangou%></B>
<TABLE width="320" border="0">
<TR>
<TD><FONT size="2"><%=strKiji%></FONT></TD>
<TD align="right" valign="bottom"><FONT size="2"></FONT></TD>
</TR>
</TABLE>
</TD>
....
I think te problem is Classloader. i am using TOMCAT3.2.1.
It seems that tomcat use different classloader to load my servlet & jsp .Even i have verfied the object class name is the same by using Reflect API , there is always a ClassCastException thrown
thousands thanks again .
ZY
 
zheng yu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry
i dont know how to post code
ZY
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No charge
ClassLoaders can be the bane of your existance, the Tomcat ClassLoader FAQ is definitely worth reading.
Dave.
 
Marcos Maia
Ranch Hand
Posts: 977
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
David coul�d you pls tell us where the url to Tomcat ClassLoader FAQ.
thanks in advance.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!