Java.Servlet.How can I handle errors in JSP pages?

In JSP, you can handle errors in two main ways:

ApproachDescription
1. Using errorPage and isErrorPage page directivesForward to a special JSP page when an error occurs
2. Using centralized web.xml error handlingMap 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 exception implicit 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 to notfound.jsp.
  • If a NullPointerException occurs, user is sent to nullpointer.jsp.
  • If any other exception happens, user is sent to generalerror.jsp.

✅ This works even outside JSPs — it covers Servlets too!

🧠 What Happens Internally?

StepAction
Error happensException thrown or HTTP error (e.g., 404)
Container checksweb.xml for mappings, or errorPage directive
Forward requestTo the specified error page
Error pageDisplays 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 access exception object in error JSP.
  • Customize error message for users (don’t show full stack trace in production).

🎯 Quick Comparison

MethodWhen to Use
errorPage directiveHandle errors locally (page-specific)
web.xml configurationHandle 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.

This entry was posted in Без рубрики. Bookmark the permalink.