java.lang.ClassCastException cannot be cast to javax.servlet.ServletException

rec picture rec · Nov 9, 2009 · Viewed 14.6k times · Source

I'm trying to deploy a java application to appspot (google appengine). I'm new to java, so bear with me. When I run the application locally from eclipse, it runs fine. After uploading it to google appspot, I get an error (only in one of the .jsp pages, other .jsp pages work fine). The error log says:

   Uncaught exception from servlet

java.lang.ClassCastException: java.lang.ClassCastException cannot be cast to javax.servlet.ServletException
    at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:754)

Can somebody shed some light on this issue? What could be wrong in this particular page? If you would like to see the page code, let me know.

Answer

jheddings picture jheddings · Nov 9, 2009

It looks like something in your code is throwing an exception that does not derive from ServletException. A handler upstream is catching that exception and (possibly) trying to do something intelligent with it.

There is probably another underlying issue causing the exception to be thrown in the first place, but that might be revealed by seeing first what the exception is.

If you aren't sure where this exception is, try wrapping you entire page handler in a try ... catch block, looking for all Throwable's. When you find one, rethrow it inside of a ServletException:

try {
    // handle page request
} catch (Throwable t) {
    throw new ServletException(t);
}

This should allow the web server to display the exception so you can continue tracking down the problem. Note that this should probably be temporary code.