Home / Articles / Customizing the Look of Error Messages in JSP

Customizing the Look of Error Messages in JSP

  • By .
  • Sample Chapter is provided courtesy of Que.
  • Date: Feb 22, 2002.

Chapter Description

Using JavaServer Pages, you can create custom error pages to deliver user-friendly messages to users when errors occur. In this article, Drew Falkman shows you how.
This sample chapter is excerpted from Drew Falkman's JRun Web Application Construction Kit (Que, 2001).

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 1—error1.jsp—A 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.

2. Setting Error Pages with the page Directive | Next Section