In the previous tutorial, we have discussed JSP Directives. Now, we have come with the new JSP Topic ie., Exception Handling in JSP. So, in this tutorial, we will learn JSP Exception Handling with Examples. Before understanding JSP Exception Handling, let’s start learning what is Exception and how it is different from errors. Just use the direct links for quick reference about the JSP Exception Types, Methods, etc.
- What is an Exception?
- Types of Exceptions in JSP
- What is Error?
- Methods of Handling Exception in JSP
- Handling Exception using page directive attributes
- Example of exception handling in JSP by the elements of page directive
- Handling Exceptions Using error-page Element in web.xml File
- Example of JSP exception handling by the error-page element in web.xml file
An exception is an event, that occurs during the execution of a program, that disrupts the normal flow of program instructions. when an error occurs within a method, the methods create objects and hands it off to the runtime system.
The object called an exception object. It contains the information about the error, including its type and state of the program when the error occurred. Exception Handling is the process of handle the errors that may occur during runtime.
There are three types of JSP Exceptions. They are as follows:
It is usually a user error or problems which are not recognized by the developer are named as checked exceptions.
Runtime exceptions are the one which could have evaded by the programmer. They are overlooked at the time of compilation.
It is an example of the throwable class, and it is used in error pages.
A few of the throwable class methods are:
- Public String getMessage() – returns the message of the exception.
- Public printStackTrace()- returns the stacktrace of the exception.
- Public throwablegetCause() – returns cause of the exception.
Most of the time errors are not caused by our program these are caused by system resources. We cannot handle the errors.
For instance: if OutOfMemory occurs being a programmer we can’t do anything and the program will be terminated automatically.
- JSP Standard Tag Library Tutorial (JSTL), JSTL Tags
- JSP Directives: Page, Include & Taglib Directive
In JSP, We can handle the exceptions in two ways. They are as such:
- By errorPage and isErrorPage attributes of page directive
- By <error-page> element in web.xml file
In JSP Page Directive, there are two attributes that help in handling exceptions in JSP. They are:
This attribute is utilized to notify the container to jump the control to another JSP page if an exception occurs in the current JSP page. Extra JSP page is used to handle the exception that is happened in the current JSP page.
Syntax: <%@ errorPage = “error.jsp” %>
This attribute is utilized to notify the container that makes a JSP an error page or not. The default value of this attribute is false. It means a JSP page is not acting as an error page by default. To make a JSP page as an error page than we should write isErrorPage = “true”. If isErrorPage = “true” then only execution objects is passed into a JSP page.
Syntax: <%@ isErrorPage=”true” %>
In this example, we are creating the 3 files.
- index.jsp: for input values.
- calculation.jsp for dividing the two numbers and display the result.
- error.jsp for handle the jsp exception.
The other way of defining the error page for each element, but in place of using the errorPage directive, the error page for each page can be specified in the web.xml file, with the help of the <error-page> element.
The syntax for <error-page> is as follows:
<web-app> <error-page> <exception-type>Type of Exception</exception-type> <location>Error page url</location> </error-page> </web-app>
The below example shows the usage of this technique to handle exceptions:
<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>
// 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 % >
// JSP code for error page, which displays the exception <%@ page isErrorPage="true" %> <h1>Exception caught</h1> // displaying the exception The exception is: <%= exception %>
<web-app> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/error.jsp</location> </error-page> </web-app>
In this case, the output is as same as the previous one.