Nearby lessons
8 of 30JSP - Error Pages (Exception Object)
- Configure error pages declaratively in web.xml
- Configure error pages programmatically using page directive
- Use the exception implicit object on error pages
Error Handling is essential for building robust JSP applications. This lesson explains how to configure error pages using the declarative and programmatic approaches, and how to use the exception implicit object.
Why Error Pages?
It is not recommended to show raw Java error information to end users. We need to convert technical exceptions into user-friendly messages. For this, we configure error pages.
There are 2 approaches to configure error pages:
- Declarative Approach — configure in
web.xml(applies to the entire application) - Programmatic Approach — use
pagedirective attributes (applies to a single JSP)
1) Declarative Approach (web.xml)
Configure error pages in web.xml — these apply to the entire web application:
2) Programmatic Approach (page directive)
Use the errorPage attribute to redirect to an error page on exception:
Error Page (isErrorPage)
The error page itself must be marked with isErrorPage="true". Only then is the exception implicit object available:
Which Approach is Recommended?
Declarative approach is recommended because:
- You can customize error pages based on exception type and error code
- The configuration is centralized in one place (web.xml)
- If you visit the error page directly (without an exception),
exceptionisnull
Implicit Object Availability Rules
| Rule | Details |
|---|---|
Implicit objects are local variables of _jspService() | Scriptlets and expressions land inside _jspService(), so they can use implicit objects |
Declarations land outside _jspService() | So they cannot use implicit objects |
session can be disabled | <%@ page session="false" %> |
exception is only on error pages | <%@ page isErrorPage="true" %> |
- Error pages convert Java exceptions into user-friendly messages
- Declarative approach (web.xml) is recommended over programmatic approach
- The exception implicit object is only available on pages marked isErrorPage="true"