Search code examples
javajspexceptionservletsjstl

How can I print error stack trace in JSP page?


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

 <error-page>
  <exception-type>java.lang.Exception</exception-type>
  <location>/errors/error.jsp</location>
 </error-page>

Now I would like to print stack trace of error on JSP (of course in development mode only). How can I print stack trace of error on my JSP page? I don't use any frameworks for this application, so only default servlet APIs are available for my program.


Solution

  • get the parameter from request that is set internally and use it to print and deal with other information like cause, message

    <c:set var="exception" value="${requestScope['javax.servlet.error.exception']}"/>
    

    and to print stacktrace

    <!-- Stack trace -->
    <jsp:scriptlet>
      exception.printStackTrace(new java.io.PrintWriter(out));
    </jsp:scriptlet>
    

    See Also