How can I print error stack trace in JSP page?

newbie picture newbie · Nov 15, 2011 · Viewed 49.5k times · Source

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.

Answer

Jigar Joshi picture Jigar Joshi · Nov 15, 2011

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