Introduction
This article describes Exception Handling In JSP.
JSP Exception Handling
An exception is an object that disrupts the normal flow and it is thrown at runtime. Exception Handling is the technique to handle the runtime errors. An exception can occur at any time in our web application. So we need to handle exceptions to provide safety for a web developer. In JSP, there are two ways to perform exception handling; they are by the use of:
- errorPage and isErrorPage attribute of page directive
- <error-page> elements in web1.xml file.
1. Handling an exception usiing an errorPage and isErrorPage attribute of the page directive
To handle an exception usiing an errorPage and isErrorPage attribute of the page directive, we must define and create a page to handle the exceptions, as in showerror.jsp. The pages where an exception may occur is defined in the errorPage attribute of the page directive, as in the inprocess.jsp pages.
There are 3 files:
- home.jsp for input values.
- inprocess.jsp for dividing the two numbers and displaying the result
- showerror.jsp for handling the exception.
home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<form action="inprogress.jsp">
First No:<input type="text" name="n1" /><br/><br/>
Second No:<input type="text" name="n2" /><br/><br/>
<input type="submit" value="divide"/></form>
</html>
inprocess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%@ page errorPage="showerror.jsp" %>
<%
String num1=request.getParameter("n1");
String num2=request.getParameter("n2");
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
%>
<% try {
int c=a/b;
out.print("division of numbers is: "+c);
}
catch(ArithmeticException aEx){
out.println("Arithmetic Exception : "+aEx);
}
catch(Exception ex){
out.println("Exception : "+ex);
}
%>
</body>
</html>
showerror.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@ page isErrorPage="true" %>
<h3>There is an exception generated!</h3>
Exception is:<%= exception %>
</body>
</html>
Output
Now we provide two inputs; the first number is 24 and the second number is 4, as in the following:
Now click on the divide button and generate the result as in the following:
Now again, provide two inputs as in the frist number of 24 and the second number of 0 to show the exception handling as in the following.
Now click on the divide button to generate an error message ("Arithmetic Exception : java.lang.ArithmeticException: / by zero ") as in the following.
2. By <error-page> elements in web1.xml file.
This approach is beneficial since we don't need to specify the errorPage attribute in each page. Specifying the single entry in the web1.xml file will handle the exception. In this case, either specify the exception-type or exception code with the location element. If we want to handle all the exceptions then we will need to specify the java.lang.Exception like java.lang.ArithmeticException in the exception-type element. Let's see the simple example:
There are 4 files.
All of the above three that are used in the previous one (in other words home.jsp, inprogress.jsp and showerror.jsp) and
web1.xml file for specifying the error-page element.
4. web1.xml for handling any type of exception
If we need to handle any exception then:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/showerror.jsp</location>
</error-page>
</web-app>
This approach is better if we need to handle any exception.
web1.xml file for a specific error-code
If we know any specific error code and we want to handle that exception then we specify the error-code element instead of exception-type as in the following.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<error-page>
<exception-code>404</exception-code>
<location>/showerror.jsp</location>
</error-page>
</web-app>
Output
The output of this one is the same as in the previous one.