Customizing the Look of Error Messages in JSP
- Creating an Error Page
- Setting Error Pages with the page Directive
- Setting Error Pages by Using the web.xml Template
The Web application framework provides a simple way to customize the look of error messages that can occur while users are accessing your pages. As you know, error messages are thrown because of syntax problems in your code, because of database connection problems, or just because the user has left out one or more required fields while filling out a form.
The application framework enables you to customize any of these error messages. You can even hide them from the user's view entirely, if you want. This enables you to maintain a consistent look and feel throughout an application, even when those dreaded error messages are thrown.
Creating an Error Page
The first step in outputting errors is to create an error page. Take a look at the error1.jsp page shown in Listing 1.
Listing 1error1.jspA Simple Error Page
<%-- Name: error1.jsp Author: Drew Falkman (iam@drewfalkman.com) Description: simple sample error page Created: 7/8/01 --%> <%-- set page as error page --%> <%@page isErrorPage="true" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Error Page</title> </head> <body> <h1>Error!!</h1> You seem to have encountered an error in an otherwise perfect Web Site. If you would like to report this error, you may email the site administrator, or call him directly. </body> </html>
There is only one distinction between this page and a regular error page:
<%@page isErrorPage="true" %>
This line allows the page to accept and display any exception information from the page that sent it. So, for example, if a file called whatever.jsp has an exception, and it is told to load error1.jsp as the error page, error1.jsp obtains all the exception information from whatever.jsp.