Bug 48582

Summary: JspServletWrapper.getServletContext() throws NullPointerException
Product: Tomcat 6 Reporter: Tim McCune <tmccune>
Component: JasperAssignee: Tomcat Developers Mailing List <dev>
Severity: major    
Priority: P2    
Version: 6.0.24   
Target Milestone: default   
Hardware: PC   
OS: Linux   

Description Tim McCune 2010-01-20 13:25:03 UTC
We're getting this exception/stack trace in a web application:

''[2010-01-15 17:52:01,381] -  ERROR [org.apache.catalina.core.ContainerBase] Exception invoking periodic operation:
        at org.apache.jasper.servlet.JspServletWrapper.getServletContext(JspServletWrapper.java:174)
        at org.apache.jasper.compiler.JspRuntimeContext.checkCompile(JspRuntimeContext.java:304)
        at org.apache.jasper.servlet.JspServlet.periodicEvent(JspServlet.java:289)
        at org.apache.catalina.core.StandardWrapper.backgroundProcess(StandardWrapper.java:668)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1571)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1580)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1580)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1580)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1560)
        at java.lang.Thread.run(Thread.java:595)

Looking at JspServletWrapper, I see that it has 2 constructors; one for JSP pages, and another for tag files.  The one for tag files has a problem in this line:

       this.config = null; // not used

because this.config is indeed used by the public method getServletContext():

public ServletContext getServletContext() {
  return config.getServletContext();

and since that method is public, you can't safely say that this.config will not be used. :)  So this means that if you create a JspServletWrapper for a JSP tag file, you can never call getServletContext() on it, or you'll generate the NPE above.  However, JspRuntimeContext does exactly that in the following block of code, from the checkCompile() method:

        Object [] wrappers = jsps.values().toArray();
        for (int i = 0; i < wrappers.length; i++ ) {
            JspServletWrapper jsw = (JspServletWrapper)wrappers[i];
            JspCompilationContext ctxt = jsw.getJspEngineContext();
            // JspServletWrapper also synchronizes on this when
            // it detects it has to do a reload
            synchronized(jsw) {
                try {
                } catch (FileNotFoundException ex) {
                } catch (Throwable t) {
                    jsw.getServletContext().log("Background compile failed",

The values in the "jsps" map are both JSP files and tag files, as seen by TagFileProcessor.loadTagFile(), which puts tag files there.

So to summarize, any JSP tag file that experiences a compilation failure inside of the checkCompile() method will seem to generate this NullPointerException.  

A secondary issue here is that if any problem occurs inside of the catch block in the code pasted above (as is currently happening), the original exception is swallowed forever.  This is the Throw From Within Finally anti-pattern (http://today.java.net/article/2006/04/04/exception-handling-antipatterns#throwFromWithinFinally) and should be fixed as well.
Comment 1 Tim McCune 2010-01-20 13:28:36 UTC
I should also have noted that after this NullPointerException occurs, we then continue to get this error over and over:

ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/d].[jsp]] Servlet.service() for servlet jsp threw exception
'java.lang.NoClassDefFoundError: org/apache/jsp/tag/meta/internal_002dtitle_tag

presumably for the JSP tag file whose compilation failed.  I don't know if that's the behavior you want, as the error is pretty cryptic.  You may want something more explicit like "compilation of this JSP failed"?
Comment 2 Tim McCune 2010-01-25 14:48:32 UTC
I just confirmed that this issue still affects the latest version (6.0.24).
Comment 3 Mark Thomas 2010-02-16 11:08:19 UTC
Fixed in 7.0.x and proposed for 6.0.x
Comment 4 Mark Thomas 2010-02-18 12:31:46 UTC
This has been fixed in 6.0.x and will be included in 6.0.25 onwards.
Comment 5 Mark Thomas 2010-03-05 12:59:59 UTC
Also fixed in 5.5.x for 5.5.29 onwards