Friday, November 16, 2007

JSP and Error 500

Java Server Page (JSP) is hardly new. I do have some minor complains with it. First of all, how come the import syntax is so different? Ok I tolerate that.
I rather not mess with custom tags. Fact: if you let people do custom tags, they WILL write horrible code with it. So if something is broken, is it your JSP? the custom tag? where is it broken? Debugging other people's code is bad enough already, I don't need extra compiled stuff to look into. Fortunately I don't deal with a lot of custom tags.

Another thing, JSP is compiled into a servlet at run time. So if you have a syntax error like forgot to close a brace. It can be a nightmare to find where did you miss it. Ok this is fairly minor. A good editor may be able to detect your errors before you run.

The real complain is a Internal Server Error 500 when something goes wrong. Oh you got a null pointer assignment somewhere? boom, 500 Error. Nothing else you can see what's happening. I want to see the stack trace! It may say something in the log. Unfortunately it may not say anything, and you may not have access to it due to stupid security settings.

All JSP programmers should know about <%=variable%> as output, and know it is equivalent to out.println(variable). This displays the variable on screen. Use this to your advantage to show your stacktrace. Waita minute, how to get the stack trace? You need a try block and the important e.printStacktrace() method. Unfortunately, e.printStacktrace() writes to System.out, not the regular "out" you want...

Fortunately printStackTrace is overloaded with a flavor that takes a stream, but you need to convert it to a PrintWriter.
Here is the trick,

try {
// suspicous code
} catch (Exception ex) {
ex.printStackTrace(new java.io.PrintWriter(out));
}

No comments: