Exception handling in JSP - GeeksforGeeks

28 Sep, 2021
Java Server Pages declares 9 implicit objects, the exception object being one of them. It is an object of java.lang.Throwable class, and is used to print exceptions. However, it can only be used in error pages.
There are two ways of handling exceptions in JSP. They are: 
By errorPage and isErrorPage attributes of page directive
By <error-page> element in web.xml file
Handling Exception using page directive attributes
The page directive in JSP provides two attributes to be used in exception handling. They’re:  
errorPage: Used to site which page to be displayed when exception occurred.Syntax :
 <%@page errorPage="url of the error page"%>
isErrorPage: Used to mark a page as an error page where exceptions are displayed. Syntax :
 <%@page isErrorPage="true"%>
In order to handle exceptions using the aforementioned page directives, it is important to have a jsp page to execute the normal code, which is prone to exceptions. Also, a separate error page is to be created, which will display the exception. In case the exception occurs on the page with the exception prone code, the control will be navigated to the error page which will display the exception.
The following is an example illustrating exception handling using page directives:
index.html  
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>
A.jsp 
Java
// JSP code to divide two numbers<% @page errorPage = "error.jsp" %> < %                                           String num1    = request.getParameter("first");String num2 = request.getParameter("second"); // extracting numbers from requestint x = Integer.parseInt(num1);int y = Integer.parseInt(num2);int z = x / y; // dividing the numbersout.print("division of numbers is: " + z); // result % >
error.jsp 
Java
// JSP code for error page, which displays the exception<% @page isErrorPage = "true" %>     <h1> Exception caught</ h1>         The exception is : <%= exception %> // displaying the exception
Output: index.html 
error.jsp 
Handling Exceptions Using error-page Element En web.xml File
This is another way of specifying the error page for each element, but instead of using the errorPage directive, the error page for each page can be specified in the web.xml file, using the <error-page> element. The syntax is as follows:
HTML
<web-app>     <error-page>   <exception-type>Type of Exception</exception-type>   <location>Error page url</location>   </error-page>     </web-app> 
The following example illustrates using this technique to handle exceptions:
index.html 
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>
a.jsp 
Java
// JSP code to divide two numbers< %         String num1    = request.getParameter("first");String num2 = request.getParameter("second");// extracting the numbersint x = Integer.parseInt(num1);int y = Integer.parseInt(num2);int z = x / y; // dividingout.print("division of numbers is: " + z); // result % >
error.jsp 
Java
// JSP code for error page, which displays the exception<%@ page isErrorPage="true" %>    <h1>Exception caught</h1>    // displaying the exceptionThe exception is: <%= exception %>
web.xml 
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. 
Akanksha_Rai
varshagumber28
Java-JSP
Java
Web Technologies
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?