Exceptions in Servlets

Whenever a Servlet throws an exception, the web container searches the configurations in web.xml that use the exception type element for a match with the thrown exception type.

We need to use error-page elements in web.xml to specify the invocation of Servlet in response to certain exceptions or HTTP status code.

web.xml Configuration

Whenever a Servlet throws an exception, the web container searches the configurations in web.xml that use the exception type element for a match with the thrown exception type.

Let us understand how to configure the web.xml file.

servlets21

Whenever we need to configure web.xml file for handling exceptions, then we need to use an <error-page> tag. And if you want to configure based on error code, then with that error-page tag, we need to use a tag with the name <error-code>. And then we need to specify the error-code, for example, 404. And then close the error-code tag. Once we’ve provided the error-code, then we need to specify the location of the Servlet to be used for handling the exception using a location tag where we need to use the url-pattern of the Servlet.

If we need to configure the error pages based on exception-types, then also we need to use <error-page>. But instead of using error-code tag, we need to use <exception-type> tag. I have specified the exception-type, for example, javax.servlet.ServletException. And then close the exception-type tag. And then to handle that exception, we need to specify the location tag with a url-pattern of the matching Servlet to handle the exception.

Generic Handler for all exceptions :

If we want to have a generic error handler for all the exceptions, then we should define the following error-page instead of defining separate error-page elements for every exception :

servlets22

We need to remember two points while configuring the web.xml.

  1. The first one, a Servlet which handles the exception will be defined in the same usual way as any other Servlet, and it will be configured within the web.xml file.
  2. The second, a single Servlet error handler page can be used to handle any number of error-code or exception-types.

Once we have an idea on the configuration details required for the web.xml file, then next we need to understand what are the details that an error handling Servlet can access. So let us understand about the request attributes related to the exceptions in the next clip.

Request Attributes – Exceptions

Whenever we create a Servlet page for handling the exceptions, then we require the details about the exceptions that are raised to analyze the nature of an error or exception.

We have been provided with a list of request attributes that an error handling Servlet can access to get the details about an exception.

javax.servlet.error.status_code – this attribute gives us a status code and returns the value in integer data type.

javax.servlet.error.exception_type – this attribute gives information about exception type and returns java.lang.class data type.

javax.servlet.error.message – this attribute gives information about the exact error message and returns a string value.

javax.servlet.error.request_uri – this attribute gives information about a URL calling the Servlet, and it returns a string data type.

javax.servlet.error.exception – this attribute gives information about the exception raised.

javax.servlet.error.servlet_name – this attribute gives a Servlet name and returns the value in a string data type.

Example : Error Handler Servlet

Now let us understand how to handle exception in Servlets practically.

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
                      throws ServletException, IOException { 
     Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); 
     String errorMessage= (String) request.getAttribute("javax.servlet.error.message"); 
     String requestURI = (String) request.getAttribute("javax.servlet.error.request_uri"); 
     String servletName = (String) request.getAttribute("javax.servlet.error.servlet_name"); 
    
     response.setContentType("text/html"); 
     PrintWriter out = response.getWriter(); 
     out.println("<html>"); 
     out.println("<head>"); 
     out.println("<title>Error Handling </title>"); 
     out.println("</head>"); 
     out.println("<body>"); 
     out.println( "<p>Status Code : " + statusCode + "</p>"); 
     out.println( "<p>Error Message : " + errorMessage + "</p>"); 
     out.println( "<p>Request URI : " + requestURI + "</p>"); 
     out.println( "<p>Servlet Name : " + servletName + "</p>"); 
     out.println("</body>"); out.println("</html>"); 
}

 

The most important step is, we need to configure the web.xml file for handling the exceptions. So let me open that web.xml file present at the WEB­INF folder.

Let me add the entry for error pages within the file. I have configured the web.xml file to support 404 and 401 error codes.

 <error-page>
    <error-code>404</error-code>
    <location>/ErrorHandlerServlet</location>
 </error-page>
 <error-page>
    <error-code>407</error-code>
    <location>/ErrorHandlerServlet</location>
 </error-page>