Exception handling in JSP

Last Updated : 22 May, 2026

Exception handling in JSP is used to manage runtime errors and show meaningful messages instead of application failure. JSP handles exceptions using page directives (errorPage, isErrorPage) and global configuration in web.xml, improving application stability and user experience.

  • Prevents unexpected application failure
  • Provides user-friendly error messages
  • Helps in debugging and maintaining applications

Why Exception Handling is Important in JSP ?

Exception handling is important in JSP because:

  • It prevents application crashes
  • It displays user-friendly error messages
  • It improves application stability
  • It helps in debugging and logging errors
  • It ensures smooth execution of web applications

Ways to Handle Exceptions in JSP

There are two main approaches to handling exceptions in JSP:

1. Using Page Directive

Handles exceptions at the individual JSP page level

  • Exception object available only in error page
  • Automatically redirects when exception occurs
  • Best for small applications

1. errorPage

  • Specifies the JSP page to redirect to when an exception occurs

Syntax:

<%@ page errorPage="error.jsp" %>

2. isErrorPage

  • Marks a JSP page as an error page
  • Allows access to the implicit exception object

Syntax:

<%@ page isErrorPage="true" %>

2. Using web.xml

Handles exceptions at the application level

  • Centralized error handling
  • Reduces code duplication
  • Recommended for large applications

Example: Using Page Directive

Step 1: Create index.html

  • Create a form to take user input
  • Send request to JSP page (e.g., a.jsp)
HTML
<html>
<head>
<body>
<form action="a.jsp">  
Number1:<input type="text" name="first" >
Number2:<input type="text" name="second" > 
<input type="submit" value="divide">  
</form>  
</body>
</html>

Step 2: Create a.jsp

  • A JSP file created in the WebContent / webapp folder .
  • It generates an exception and redirects to an error page using errorPage.
Java
<%@ page errorPage="error.jsp" %>
<%
String num1 = request.getParameter("first");
String num2 = request.getParameter("second");

int x = Integer.parseInt(num1);
int y = Integer.parseInt(num2);
int z = x / y;

out.print("Division result: " + z);
%>

Step 3: Create error.jsp

  • A JSP error page created in the WebContent / webapp folder .
  • Handles and displays exception details using isErrorPage.
Java
<% @page isErrorPage = "true" %>
<h1> Exception caught</ h1>
The exception is : <%= exception %> 

Output:

  • Valid input -> Division result displayed
  • Division by zero / invalid input -> Redirected to error.jsp

index.html 

error.jsp 

2. Exception Handling Using <error-page> in web.xml

Instead of defining error pages in each JSP, we can define them centrally in web.xml.

Syntax

<error-page>
<exception-type>ExceptionClass</exception-type>
<location>/error.jsp</location>
</error-page>

Example: Using web.xml

Step 1: Create a.jsp 

  • A JSP file created in the WebContent / webapp folder .
  • Throws an exception without defining an error page locally.
Java
<%
String num1 = request.getParameter("first");
String num2 = request.getParameter("second");

int x = Integer.parseInt(num1);
int y = Integer.parseInt(num2);
int z = x / y;

out.print("Division result: " + z);
%>

Step 2: Create error.jsp

  • A global error handling JSP file created in the WebContent / webapp folder
  • Displays exception information.
Java
<%@ page isErrorPage="true" %>
<h1>Exception Caught</h1>
The exception is: <%= exception %>

Step 3: Configure web.xml

  • A deployment descriptor created in the WEB-INF folder.
  • Defines global exception handling for the entire application.
HTML
<web-app>  
  
 <error-page>  
  <exception-type>java.lang.Exception</exception-type>  
  <location>/error.jsp</location>  
  </error-page>  
   
</web-app>  

The output, in this case, is similar as in the previous one.

Explanation

  • All exceptions of type java.lang.Exception are handled
  • No need to define errorPage in each JSP
  • Recommended for large applications

Comparison of Both Approaches

Feature

Page Directive

web.xml

Scope

Page-level

Application-level

Reusability

Low

High

Configuration

Inside JSP

Centralized

Best for

Small apps

Large apps

Comment