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:

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

    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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


A.jsp

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


error.jsp

filter_none

edit
close

play_arrow

link
brightness_4
code

// JSP code for error page, which displays the exception
<% @page isErrorPage = "true" %>
  
    <h1> Exception caught</ h1>
  
        The exception is : <%= exception %> // displaying the exception

chevron_right


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 eah page can be spefied in the web.xml file, using the <error-page> element. The syntax is as follows:

filter_none

edit
close

play_arrow

link
brightness_4
code

<web-app>  
    
 <error-page>  
  <exception-type>Type of Exception</exception-type>  
  <location>Error page url</location>  
  </error-page>  
     
</web-app>  

chevron_right


The following example illustrates using this technique to handle exceptions:

index.html

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


a.jsp

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


error.jsp

filter_none

edit
close

play_arrow

link
brightness_4
code

// JSP code for error page, which displays the exception
<%@ page isErrorPage="true" %>  
    
<h1>Exception caught</h1>  
    
// displaying the exception
The exception is: <%= exception %>

chevron_right


web.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

<web-app>  
    
 <error-page>  
  <exception-type>java.lang.Exception</exception-type>  
  <location>/error.jsp</location>  
  </error-page>  
     
</web-app>  

chevron_right


The output, in this case, is similar as in the previous one.



My Personal Notes arrow_drop_up

Intern Technical Content Writing at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.