Global Exception configuration using web.xml, servlet and jsp

This tutorial shows how we can configure exception/error handling globally using deployment descriptor, servlet and jsp. The aim of this tutorial is to teach whenever an exception or error occurrs in an web based application then that exception or error will be thrown in an appropriate manner and show the error page with the exception or error information in jsp page. For this we do not need to create as many jsp pages or servlets as many exceptions occur rather we will create one servlet for capturing exceptions or errors and one jsp page for displaying error informations to the user from the exceptions and put neccessary configurations for the exceptions in web.xml file.

We can provide link to the application home page or some details about the error information to let user know what went wrong in the application.

We can have multiple servlets depending upon the type of exceptions or errors for the application but for simplicity I will create only one servlet and use it for both exceptions and errors.

Below is the ExceptionHandler servlet source code

 

Let’s configure the deployment descriptor file

 

As you can see in the above source code, it’s very easy to specify ExceptionHandler servlet for the application using error-page element. Each error-page element should have either error-code or exception-type element. We define the ExceptionHandler servlet in location element.

 

Based on above configuration, if the application throws 404 error, 403 error or any kind of exception then it will be handled by ExceptionHandler servlet.

We can also configure different error page for different exception type such as:

but I have used

 

for all kind of exception handling in one line.

When such exception or error scenario occurrs, servlet container will invoke the corresponding HTTP method of the ExceptionHandler servlet and pass the request and response object. Here I have provided implementation for both doGet() and doPost() methods using a common method cathException() to handle the exceptions or errors in both methods.

Before servlet container invokes the servlet to handle the exception, it sets some attributes in the request to get useful information about the exception, some of them are javax.servlet.error.exception, javax.servlet.error.status_code, javax.servlet.error.servlet_name and javax.servlet.error.request_uri.

Now when we hit our servlet or any url that is throwing Exception, we get a response like the below:

global_exception_handling_using_servlet_jsp_web

global_exception_handling_using_servlet_jsp_web

view file – error/error.jsp where exception or error information is displayed.

 

view file – index.jsp which is a home page

 

Thanks for your reading. Please leave a comment if you have any query.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.