Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Page - how to print stack trace in JSP

I've created exception handling in my Spring application using spring SimpleMappingExceptionResolver. Everything works fine. Now I need to somehow print the caught exception within the jsp page. Something like message and stack trace. In my jsp I've found the exception object in "exception" attribute. All I need to do is something like that:

${exception.printStackTrace()}

But I don't know how. Is there any way how to do that?:-)

Thanks for any suggestion,

Mateo

like image 491
mateo Avatar asked Dec 14 '10 17:12

mateo


People also ask

How do I print a stack trace error?

The printStackTrace() method of Java. lang. Throwable class used to print this Throwable along with other details like class name and line number where the exception occurred means its backtrace. This method prints a stack trace for this Throwable object on the standard error output stream.

How do I display error messages in the same JSP page?

To create a JSP error page, we need to set page directive attribute isErrorPage value to true, then we can access exception jsp implicit object in the JSP and use it to send customized error message to the client.


1 Answers

The easiest solution I can think of is to loop over the stack trace elements, taking advantage of the Throwable.getStackTrace() method:

<c:forEach items="${exception.stackTrace}" var="element">
    <c:out value="${element}" />
</c:forEach>

You'd need to add some formatting, of course.

like image 128
skaffman Avatar answered Sep 30 '22 19:09

skaffman