In JSP, you can handle errors in two main ways:
| Approach | Description |
|---|---|
1. Using errorPage and isErrorPage page directives | Forward to a special JSP page when an error occurs |
2. Using centralized web.xml error handling | Map errors globally to error pages (by status code or exception type) |
✅ You can use one or both approaches, depending on how big your project is!
🔵 1. Handle Errors Using errorPage and isErrorPage Attributes
This is local JSP-level error handling.
📜 Step 1: Mark the Main JSP
At the top of your main JSP page (where an error might happen), you declare:
<%@ page errorPage="error.jsp" %>
If an exception occurs inside this page, the request is automatically forwarded to error.jsp.
📜 Step 2: Create the Error JSP Page
At the top of error.jsp, declare:
<%@ page isErrorPage="true" %>
- This allows access to the
exceptionimplicit object.
Inside error.jsp, you can show error details:
<html>
<body>
<h2>An error occurred:</h2>
<p>Exception: ${exception}</p>
<p>Message: ${exception.message}</p>
</body>
</html>
✅ exception is an implicit object of type Throwable.
🔵 2. Handle Errors Globally Using web.xml
This is application-wide centralized error handling.
You can configure specific error codes or exception classes.
📜 Example: Define in web.xml
<error-page>
<error-code>404</error-code>
<location>/errors/notfound.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/errors/nullpointer.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errors/generalerror.jsp</location>
</error-page>
- If a page is not found (
404), the user is sent tonotfound.jsp. - If a
NullPointerExceptionoccurs, user is sent tonullpointer.jsp. - If any other exception happens, user is sent to
generalerror.jsp.
✅ This works even outside JSPs — it covers Servlets too!
🧠 What Happens Internally?
| Step | Action |
|---|---|
| Error happens | Exception thrown or HTTP error (e.g., 404) |
| Container checks | web.xml for mappings, or errorPage directive |
| Forward request | To the specified error page |
| Error page | Displays error information (optionally using ${exception}) |
🛠️ Important Notes:
- Error pages must be outside of public access (e.g., place them inside
/WEB-INF/) if you don’t want users to access them directly. - Use
isErrorPage="true"if you want to accessexceptionobject in error JSP. - Customize error message for users (don’t show full stack trace in production).
🎯 Quick Comparison
| Method | When to Use |
|---|---|
errorPage directive | Handle errors locally (page-specific) |
web.xml configuration | Handle errors globally (for the whole app) |
🚀 Example Full Setup
Main page (main.jsp):
<%@ page errorPage="error.jsp" %>
<%
int x = 5 / 0; // This will throw an exception
%>
Error page (error.jsp):
<%@ page isErrorPage="true" %>
<html>
<body>
<h1>Something went wrong!</h1>
<p>Error Type: ${exception}</p>
<p>Message: ${exception.message}</p>
</body>
</html>
✅ If an exception occurs in main.jsp, the server automatically forwards to error.jsp and displays the error info.