Thursday, July 26, 2007

How to Turn a Java Stack Trace into a String (mostly for JSTL)

I had an exception, myException, caught using c:catch. Here's how I turned it into a String and made it accessible in the JSP a stackTrace:

import java.io.StringWriter;
Exception myException = (java.lang.Exception) pageContext.getAttribute("myException");
StringWriter sw = new StringWriter();
myException.printStackTrace(new PrintWriter(sw));
String stackTrace = sw.toString();
pageContext.setAttribute("stackTrace", sw);

No comments:

Labels

Blog Archive

Contributors