aspose file tools*
The moose likes JSP and the fly likes Explain this ClassCastException, Please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Explain this ClassCastException, Please" Watch "Explain this ClassCastException, Please" New topic
Author

Explain this ClassCastException, Please

Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

I'm working on a shopping cart for a company. My JSP pages are simply HTML with java code embedded using <% and %> -- not the most sophisticated, but I'm still learning--and the deadlines too tight for me to go back and redo these now
Anyway, I'm receiving the following error:

The offending lines of code are:

Obviously, the extra verbage was me trying to make sense out of this; so far, it's only served to confuse me. Why would I be getting a ClassCastException when the target object is of the expected class?
This is particularly annoying because this all tested out on my machine fine, but when I moved to the webserver I've been getting all sorts of errors. I just recently noticed that the server is running Tomcat 3.2.3 while I'm running 4.1--But would the difference in the versions account for this


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Peter Storch
Ranch Hand

Joined: Jun 12, 2003
Posts: 74
Can you check if the class is available more than once in your classpath, e.g. WEB-INF/classes, WEB-INF/lib, <some_server>/lib or on the system classpath?
This error can occour if the same class is loaded by different class loaders.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

It doesn't seem to be in multiple places--it only resides in WEB-INF/classes; everything in WEB-INF/lib is eithier things like Java Mail API jars or (my) third party jar files.
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Also, that would be my suggestion.
That there is conflict(i.e. multiple versions) on the class:
com.silverlake.database.Order
which is causing your ClassCastException.
Craig
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Hmm...I've been thinking, but correct me if I'm wrong.
Timeline:
1. Develop original application. Upload to Web site. Find some problems that exist only on web server.
2. Make some changes, specifically to the Order class and the cart.jsp file (the one that is giving the error message). Also change the OrderBookServlet, which places the order in the session. UPload these changes.
3. Tomcat detects changes to cart.jsp and recompiles, using new version of class Order that was uploaded. The OrderBookServlet, however, is not reloaded. It is still using the old version of the class.
4. New Version != old version. ClassCastException.
So:
1). How do I check the versions of a class being used so that I can verify this (I guess the question is "Can I check...").
2). Is there anyway to get Tomcat to reload the Servlets short of having to get the server restarted (which requires going through tech support....)?
Peter Storch
Ranch Hand

Joined: Jun 12, 2003
Posts: 74
Originally posted by Joel McNary:

1). How do I check the versions of a class being used so that I can verify this (I guess the question is "Can I check...").

I think you are right with your problem description. If the Servlet container just reloaded the changed classes but there are already used instances of it than this problem seems reasonable. But if you can check this at runtime? Huh, that's a tricky question. I don't think that's possible, but I'm also interessted in a solution.
Originally posted by Joel McNary:
2). Is there anyway to get Tomcat to reload the Servlets short of having to get the server restarted (which requires going through tech support....)?[/QB]

I think in Tomcat 4.x you can restart a single webapp without the need of restarting the whole tomcat. But if you don't have access to the manager app, I think you have to bother the tech support.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
If you delete everything in ${TOMCAT_HOME}/work/Standalone/${hostname}/${appname} , won't that force recompilation and restarting of all JSPs in the app?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Peter Storch
Ranch Hand

Joined: Jun 12, 2003
Posts: 74
Originally posted by Ron Newman:
If you delete everything in ${TOMCAT_HOME}/work/Standalone/${hostname}/${appname} , won't that force recompilation and restarting of all JSPs in the app?

Hmmm, and what about the existing instances of a class "living" in the HTTP session? I don't think the HTTP sessions are destroyed during jsp recompilation. I really think you need to restart the webapp, or wait until all sessions are timedout.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

There is a 'horrible-hack' solution to this problem that involves getting Object streams joined by a Piped stream, passing the offending object in one end, pulling it out the other and doing the cast at that point, but it is much preferable to stop the problem from happening in the first place than patching it.
Peter Storch
Ranch Hand

Joined: Jun 12, 2003
Posts: 74
Originally posted by David O'Meara:
There is a 'horrible-hack' solution...

Uhh, that sounds really horrible
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explain this ClassCastException, Please