How can I print error stack trace in JSP page?

后端 未结 9 2149
说谎
说谎 2020-12-01 17:50

I have set my error page like this in web.xml:

 
  java.lang.Exception
  /erro         


        
9条回答
  •  一生所求
    2020-12-01 18:35

    Using JSP scriptlets is a frowned upon practice since a decade. You'd best avoid it.

    If you're already on EL 2.2 or newer (Tomcat 7+, JBoss AS 6+, WildFly, GlassFish 3+, etc), with new support for method expressions of the form ${instance.method()}, then you can just use 100% EL for this.

    First you need to explicitly flush the JSP writer via JspWriter#flush(), so that all preceding JSP template output really gets written to the writer of the servlet response:

    ${pageContext.out.flush()}
    

    Then you can just pass ServletResponse#getWriter() to Throwable#printStackTrace().

    ${exception.printStackTrace(pageContext.response.writer)}
    

    Complete example:

    <%@page pageEncoding="UTF-8" isErrorPage="true" %>
    ...
    
    ${pageContext.out.flush()}${exception.printStackTrace(pageContext.response.writer)}

    If you're already on EL 3.0 (Tomcat 8+, WildFly, GlassFish 4+, etc), you can even make it a single expression with the new semicolon operator which separates EL statements:

    <%@page pageEncoding="UTF-8" isErrorPage="true" %>
    ...
    
    ${pageContext.out.flush();exception.printStackTrace(pageContext.response.writer)}

    If you can't use isErrorPage="true" for some reason (and thus ${exception} implicit object isn't available), then just substitute with ${requestScope['javax.servlet.error.exception']}:

    <%@page pageEncoding="UTF-8" %>
    ...
    
    ${pageContext.out.flush()}${requestScope['javax.servlet.error.exception'].printStackTrace(pageContext.response.writer)}

    If you're still not on EL 2.2, then your best bet is creating a custom EL function. Detail can be found in What is the good approach to forward the exception from servlets to a jsp page?

    Below is a more complete error page example with more detail:

    <%@page pageEncoding="UTF-8" isErrorPage="true" %>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    ...
    
    • Exception:
    • Exception type:
    • Exception message:
    • Request URI:
    • Servlet name:
    • Status code:
    • Stack trace:
      ${pageContext.out.flush()}${exception.printStackTrace(pageContext.response.writer)}

提交回复
热议问题