1

I am using servelets to design my appliction . Can any one suggest me how to write the Exception message in output page ? example if I any sql exception is thrown I want to diplay the printStackTrace() method message in output page(i.e can be html or jsp) The following is the code .

exceptionObject.printStackTrace();

Regards, Raj

Udo Held
  • 12,314
  • 11
  • 67
  • 93
  • Possible duplicate: http://stackoverflow.com/questions/1149703/stacktrace-to-string-in-java – Udo Held Dec 04 '11 at 09:11
  • Probably this one is a more direct question to the same topic: http://stackoverflow.com/questions/8135980/how-can-i-print-error-stack-trace-in-jsp-page – Udo Held Dec 04 '11 at 10:46

2 Answers2

2

In jsp there is standard way of handling exception, instead of filling the output page with exception you should consider displaying the error page.

In jsp you mark an error page by setting error directive

<%@ page isErrorPage='true' %>

Once done you have the implicit object exception with you use it to display the stacktrace or whatever you want to do.

mprabhat
  • 20,107
  • 7
  • 46
  • 63
0

How can I convert a stack trace to a string?:

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
sw.toString(); // stack trace as a string

For output use:

<%= sw.toString() %>

Or check: How can I print error stack trace in JSP page?

Community
  • 1
  • 1
Udo Held
  • 12,314
  • 11
  • 67
  • 93