Error pages get an extra object: exception

An error page is essentially the JSP that handles the exception, so the Container gives the page an extra object for the exception. You probably won’t want to show the exception to the user, but you’ve got it. In a scriptlet, you can use the implicit object exception, and from a JSP, you can use the EL implicit object ${pageContext.exception}. The object is type java.lang.Throwable, so in a script you can call methods, and with EL you can access the stackTrace and message properties.

A more explicit ERROR page (“errorPage.jsp”)

image with no caption

Note

Note: the exception implicit object is available ONLY to error pages with an explicitly-defined page directive: <%@ page isErrorPage=”true” %>

In other words, configuring an error page in the DD is not enough to make the Container give that page the implicit exception object!

What happens when you request “badPage.jsp”

image with no caption

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.