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 request int x = Integer.parseInt(num1); int y = Integer.parseInt(num2); int z = x / y; // dividing the numbers out.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 numbers int x = Integer.parseInt(num1); int y = Integer.parseInt(num2); int z = x / y; // dividing out.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 exception The 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.