JSPs as error pages

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

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() %> 

<% 

   } 

%>