java.lang.StackOverflowError when a JSP request hits an exception

Solution Verified - Updated

Environment

  • JBoss Enterprise Application Platform (EAP)

Issue

  • When accessing a JSP, it fails in a StackOverflow. The stack trace provided with the exception shows deep recursion in a couple of points:
ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/workbench].[jsp]] (http-0.0.0.0-8080-1) Servlet.service() for servlet jsp threw exception
java.lang.StackOverflowError
	at org.apache.catalina.connector.RequestFacade.getAttribute(RequestFacade.java:258)
	at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:222)
	at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:222)
	at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:222)
	at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:222)
	at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:222)
        ...
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:638)
	at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:444)
	at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:382)
	at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:310)
	at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:696)
	at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:667)
	at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:808)
	at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
	at org.apache.jsp.WEB_002dINF.pages.common.globalError_jsp._jspService(globalError_jsp.java:590)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:638)
	at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:444)
	at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:382)
	at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:310)
	at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:696)
	at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:667)
	at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:808)
	at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
	at org.apache.jsp.WEB_002dINF.pages.common.globalError_jsp._jspService(globalError_jsp.java:590)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Resolution

  • Fix the initial error causing the request to be directed to the infinitely recursive error page.
  • It's best to use very simple error pages that are not likely to experience errors themselves when accessed.
  • Ensure error pages used do not refer back to themselves through the errorPage tag.
  • Ensure error pages used do not reference through the include tag other files that then use the errorPage tag to point to this page.

Root Cause

  • An error page is referring to itself as its error page through JSP errorPage tags:

      <%@ page errorPage="../globalError.jsp" %>
    
  • An error page includes another jsp file, which references back to this same error page through an errorPage tag:

      <%@ include file="../include.jsp" %>
    

    The error page inherits the errorPage setting from the included JSP.

  • An initial error sends a request to the error page. If this error page refers to itself and hits an error, it will cause infinite recursive calls as each subsequent call passes back through the filter and passes back to the error page again. Thus, HttpRequest objects are deeply wrapped and attribute calls to them result in deeply recursive calls as well.

Diagnostic Steps

  • Check the error with this StackOverflowError.

  • Check the error page being hit. Does it include an errorPage?

  • Check any files being included by the error page. Do any included files here refer themselves back to this page through the errorPage tag?

  • Check the compiled vesion of the jsp. Does it pass an error page into the JSPFactory?

      pageContext = _jspxFactory.getPageContext(this, request, response, "../common/globalError.jsp", true, 8192, true);
    
  • Replace the error page with a very simple one to avoid the problematic recursion and get to the underlying error resulting in the initial forward to the error page.

Components
Tags

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.