JSP Exception Handling
In this section we will learn how exceptional events can occur in a JSP page and how to catch these exceptional events to display a more useful message to the user. Exception Handling in JSP is much easier than Java exception handling.
JSP 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 three ways of handling exceptions in JSP —
1. Using normal try..catch block
2. Using errorPage and isErrorPage attributes of page directive
3. Using <error-page> tag in deployment descriptor (i.e. web.xml)
We will describe each of them one-by-one.
1. Using normal try..catch block
We can catch exceptions in a JSP page like we would do in other Java classes normally. We just place the code which can throw an exception between a try..catch block.
<% try { //Code which can throw can exception } catch (Exception e) { //Exception handler code here } %>
2. Using ‘errorPage‘ and ‘isEerrorPage‘ attributes of page directive
There is yet another useful way of catching exceptions in JSP pages. We can specify error page in the page directive. Then if any exception is thrown, the control will be transferred to that error page where we can display a useful message to the user about what happened and also inform us about this exception depending obviously on how important it may be. Here we will explore this error page method of exception handling in JSP pages.
Example of errorPage and isErrorPage attributes
The errorPage
attribute in a page directive informs the web container that if an exception occurs in the current page, forward the request to the specified error page.
<%@page errorPage = "error.jsp" %>
The isErrorPage
attribute in a page directive assigns a JSP page as an error page.
<%@ page isErrorPage="true" %>
The example codes are placed within JspError sub-directory under webapps.
input.html
<html> <head> <title>Division of Two Numbers</title> </head> <body> <form action="divide.jsp" method="post"> <b>Number1: </b><input type="text" name="first" ><br><br> <b>Number2: </b><input type="text" name="second" ><br><br> <input type="submit" value="Divide"> </form> </body> </html>
divide.jsp
<%@page errorPage = "error.jsp" %>
<%!
String num1, num2;
int a, b, c;
%>
<%
String num1 = request.getParameter("first");
String num2 = request.getParameter("second");
a = Integer.parseInt(num1);
b = Integer.parseInt(num2);
c = a / b;
out.print("Result is: " + c);
%>
error.jsp
<%@ page isErrorPage = "true" %>
<h3> Exception caught!</h3>
<font color="red"><b>Exception occurred = </b></font>
<font color="blue"><%= exception %></font>
Output:
On clicking the button it will move to the divide.jsp page, which in turn causes the control to be passed to error.jsp page internally and showing an exception message like following:
3. Using <error-page> tag in deployment descriptor
We may also declare error pages in the deployment descriptor (DD) for the entire web application. Using <error-page>
tag in the DD (i.e. web.xml) we can declare an error page for all types of exceptions. See the contents of web.xml file below.
<web-app>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
</web-app>
Example of codes using <error-page> tag
input.html
<html> <head> <title>Division of Two Numbers</title> </head> <body> <form action="divide.jsp" method="post"> <b>Number1: </b><input type="text" name="first" ><br><br> <b>Number2: </b><input type="text" name="second" ><br><br> <input type="submit" value="Divide"> </form> </body> </html>
divide.jsp
<%! String num1, num2; int a, b, c; %> <% String num1 = request.getParameter("first"); String num2 = request.getParameter("second"); a = Integer.parseInt(num1); b = Integer.parseInt(num2); c = a / b; out.print("Result is: " + c); %>
error.jsp
<%@ page isErrorPage = "true" %>
<h3> Exception caught!</h3>
<font color="red"><b>Exception occurred = </b></font>
<font color="blue"><%= exception %></font>
Output:
The output, in this case, is similar as in the previous one.