Open In App

Exception handling in JSP

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: 



Handling Exception using page directive attributes

The page directive in JSP provides two attributes to be used in exception handling. They’re:  



 <%@page errorPage="url of the error page"%>
 <%@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>
<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 




// 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 




// 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:




<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>
<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 




// 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 




// 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 




<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.
 


Article Tags :