Error pages in Web Application

When something bad happen in a JSP page, the user gets an error page with the stack trace leading to the error. That's helpful when we are in development phase, but it would be quite annoying in production.

So we should create specific error pages that would make the crash experience less painful.

We create an error page specifying the isErrorPage attribute as true in a page directive:
<%@ page isErrorPage="true" %>
<html><body>
    <strong>Catch all error page</strong>
</body></html>
Then we specify the error page we want to be redirect in case of troubles using the errorPage attribute in the page directive for the affected JSP page:
<%@ page errorPage="error.jsp" %>
<html><body>
About to be bad...
<% int x = 10/0; %>
</body></html>
If we try to access this page, we get a divide by zero exception, so we get redirected to the specified error page.

This is a good approach for a specific page, but it is a nightmare for a web application. In this case it would be better to specify a catch-all error page in our Deployment Descriptor. By the way, remember that it defines error pages not only for JSP pages, but for all entities in the web application:
<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/error/all.jsp</location>
</error-page>
Theorically speaking, the error page mechanism in the DD should work like a Java try-catch, so we could filter the execption, and redirect it to a specific error page. Adding this block (above the error-page for Throwable definition) we should redirect the divide by zero exception to a specific page, leaving all the other cases to the generic error page:
<error-page>
    <exception-type>java.lang.ArithmeticException</exception-type>
    <location>/error/arithmetic.jsp</location>
</error-page>
Bad news is that I checked it for a while (Windows7 - Tomcat7) but it doesn't work. Throwable eats all the exceptions. I've checked on the net, and it looks like other guys had the same issue with other Tomcat versions, and I have currently found no good solution apart from the radical one suggested in tutorialspoint.com: choose between one generic error page, or implementing all specific pages.

Even if you decide to show just a generic page, you could give some information to the kind of exception that generated the trouble, since the exception itself is made available to the error page. You can print something like that:
You caused a ${pageContext.exception} on the server.
We can specify a custom error page based on the HTTP status code, like this:
<error-page>
    <error-code>404</error-code>
    <location>/error/404.jsp</location>
</error-page>
Another source of problems is Internet Explorer, that uses its own error pages, if the provided ones are too short for its taste. Solving this issue is easier: go to Tools, Options, Advanced and uncheck the "Show Friendly Errors" ("Mostra messaggi di errore HTTP brevi" - if you have an Italian version of this browser) checkbox.

A section in chapter nine of Head First Servlet and JSP is dedicated to error handling in web application.

No comments:

Post a Comment