Wednesday, 6 July 2011

Tomcat JSP Compilation Exception

I was recently porting a simple web-app from Weblogic to Tomcat 7. Everything seemed fine, and it all deployed correctly. However, when I ran a simple test, by requesting the first page, I got the following exception:
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 38 in the generated java file
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

Stacktrace:
 at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:95)
 at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
 at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
 at org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
 at org.apache.jasper.compiler.Compiler.compile(Compiler.java:352)
 at org.apache.jasper.compiler.Compiler.compile(Compiler.java:339)
 at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:594)
 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:344)
etc...
The problem was caused by a couple of mis-matched libraries. For Weblogic, my Mavern POM file had to contain the following JAR references:
<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
    </dependency>
It turns out that Tomcat has its own copies of these JAR files in its lib directory and hence the solution is to remove these dependencies from your POM and to use Tomcat’s versions. I’m using the lastest versions available from Mavern Central, so are these out of date, or is Tomcat using obsolete JARs?

No comments: