How do I create a JSP error page to handle particular error codes?
Author: Deron Eriksson
Description: This Java tutorial describes how to create a JSP to handle particular error codes.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0) || Tomcat 5.5.20
When a particular error code occurs in the context of your JavaSW web application, such as a 404 (Not Found) or 500 (Internal Server Error), it is possible to send the user to an error page. The error code and the page location where the user should be sent in the event of that error can be specified in an error-page section in web.xmlW. Below, we see that when a 404 error occurs, the user is to be redirected to the error-404.jsp.
Section of web.xml
<error-page> <error-code>404</error-code> <location>/error-404.jsp</location> </error-page>
For this example, I created a simple error-404.jsp. The isErrorPage="true" indicates that this is an error page. This is more significant if we're displaying exception information, since the isErrorPage="true" makes available an 'exception' object for use on the error page.
<%@ page isErrorPage="true"%> Oops! A 404 error happened because the resource could not be found.
If I try to hit a resource that does not exist in my web application, I'll see the error page, as expected.
(Continued on page 2)