Home > Articles > Programming > Java

Customizing the Look of Error Messages in JSP

  • Print
  • + Share This
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).
This chapter is from the book

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">

  <title>Error Page</title>

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.


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.

  • + Share This
  • 🔖 Save To Your Account