Better error reporting with Websphere 404/500 error .jsp page

It is always a pain to detect J2EE errors. Here is a code snippet for a 404/500 type JSP error file to give you a better look at generic errors.


// <%@ page import="com.ibm.websphere.servlet.error.ServletErrorReport,java.io.*" %>
// <%
// Place in a 404/500 JSP error page file.
if (pageContext.getAttribute("ErrorReport", PageContext.REQUEST_SCOPE) != null) {
// Better error reporting in a J2EE/Websphere application.
ServletErrorReport errReport = (ServletErrorReport) pageContext.getAttribute("ErrorReport",
PageContext.REQUEST_SCOPE);

errReport.printStackTrace();
out.println("Error Code: " + errReport.getErrorCode());
if (errReport.getRootCause() != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
if (errReport.getRootCause() instanceof ServletException) {
ServletException se = (ServletException) errReport.getRootCause();
se.printStackTrace();
if (se.getMessage() != null) {
out.println((se.getMessage()));
} else {
out.println(errReport.getMessage());
} // End of If
if (se.getRootCause() != null) {
se.getRootCause().printStackTrace(pw);
} else {
se.printStackTrace(pw);
} // End of If
} else {
errReport.getRootCause().printStackTrace(pw);
out.println(errReport.getMessage());
}
pw.flush();
out.println(sw.toString());
}
else {
// Populate the StringWriter
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
StackTraceElement elem [] = errReport.getStackTrace();
for (int i = 0; i < errReport.getStackTrace().length; i++) {
out.println("<br />ERR: (rootcause==null) SE: /"
+ elem[i].getFileName() + " /" + elem[i].getClassName() + " /" + elem[i].getMethodName());
}
out.println("<br/>ERR: (rootcause==null)|" + errReport.getMessage());
out.println("<br/>ERR: (rootcause==null)|" + errReport.getStackTrace().toString());
}
}
// %>

Comments

Popular posts from this blog

On Unit Testing, Java TDD for developers to write

Is Java the new COBOL? Yes. What does that mean, exactly? (Part 1)

JVM Notebook: Basic Clojure, Java and JVM Language performance