Using an error page JSP

If you specify a JSP as the error page, you can use its implicit exception variable to obtain information about the exception. The exception variable is of type java.lang.Throwable and is initialized to the throwable reference when the uncaught exception is thrown.

To specify an error page for a JSP, set its errorPage attribute to the URL of the error page in a page directive:

<%@ page errorPage="ErrorPage.jsp" %>

To define a JSP as an error page, set its isErrorPage attribute to true in a page directive:

<%@ page isErrorPage="true" %>

This sample error page JSP uses the exception variable’s toString method to return the name of the actual class of this object and the result of the getMessage method for the object. If no message string was provided, toString returns only the name of the class.

The example also uses the getParameterNames and getAttributeNames methods of the request object to obtain information about the request.

<%@ page language="java" import="java.util.*"
	isErrorPage="true" %>
<H1 align="Center">Exceptions</H1>
<br><%= exception.toString() %>
<%! Enumeration parmNames; %>
<%! Enumeration attrNames; %>
<br>Parameters:
<%	parmNames = request.getParameterNames();
	while (parmNames.hasMoreElements()) {
%>
	<br><%= parmNames.nextElement().toString() %>
<%
	}
%>
<br>Attributes:
<%	attrNames = request.getAttributeNames();
	while (attrNames.hasMoreElements()) {
%>
		<br><%= attrNames.nextElement().toString() %>
<%
	}
%>