Justin
Justin

Reputation: 4477

JSP Error Page produces a JSP error

My spring-mvc web application has an error page with a collapsible stack trace element in it (obviously a development/debug option). This used to work just fine, and displayed the proper stack trace from the controller (or sometimes from the JSP engine).

Q: Is it possible to have a JSP be an error page, or should it be static HTML?

Q: What am I doing wrong?

The chain of events is (I think) driven by an error handler defined in my web.xml:

<error-page>
  <exception-type>java.lang.Throwable</exception-type>
  <location>/view/errors/internalError.jsp</location>
</error-page>

Somewhere along the way (switched to a maven build system, upgraded jetty, and spring 2.5 -> 3.0), I noticed that this error page stopped working the way it had been. It renderes the same with one problem -- the exception stack-trace displayed is NOT from the exception thrown in the controller, but from the error page itself!

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: 
500 /WEB-INF/jsp/admin/errors/internalError.jsp
    at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
    at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
    at org.apache.jsp.WEB_002dINF.jsp.admin.defaultParent_jsp._jspService(defaultParent_jsp.java:225)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

What seems to be happening here, is the following chain of events:

  1. Spring controller hits an uncaught exception: throw new RuntimeException("no bananas")
  2. This exception is caught by springs base MultiActionController (since no custom error handler is defined for this page) the exception is re-thrown: only to be caught again by spring's DispatcherServlet.
  3. The DispatcherServlet then looks at something (probably my web.xml) to discover the proper error page for the type of exception:
  4. Spring then resolves this URL to a view (and my error JSP) and tries to render it.
  5. Now here something goes wrong with the default parent jsp (* see below), throwing a JSP exception which gets caught, this time, by jetty's ServletHolder. This exception is now mapped (via web.xml) to the error JSP page.
  6. Here you might think that this would result in an infinite loop. However, the second time around things work out just fine and it renders the JSP error.

The JSP engine seems to die when rendering the default parent JSP -- it seems to run into trouble in: org.apache.taglibs.standard.tag.common.core.ImportSupport. I've seen some web posts where people say it has to do with the import being from a WEB-INF directory.

// disallow inappropriate response codes per JSTL spec
if (irw.getStatus() < 200 || irw.getStatus() > 299) {
  throw new JspTagException(irw.getStatus() + " " + stripSession(targetUrl));
}

Though it looks here like any HTTP 500 error would break it. But is that not what error the page should have?

Upvotes: 2

Views: 3379

Answers (1)

BalusC
BalusC

Reputation: 1108577

Q: Is it possible to have a JSP be an error page, or should it be static HTML?

It's perfectly fine. It can even be a servlet or something. As long as it doesn't have bugs of course.

Q: What am I doing wrong?

Your error page has a bug. You're apparently using <c:import> inside the error page to include a fragment from /WEB-INF. This is disallowed because it's not a public resource (honestly I don't recall that it would ever have worked in ancient JSTL versions). Rather use <jsp:include>.

Upvotes: 2

Related Questions