Servlets – Exception Handling

When a servlet throws an exception, the control goes to the web container who searches the web.xml for the exception-type tag under the <error-page> tag. If the exception which was thrown comes under the exception which is declared under that exception-type tag then the control goes to the location tag under the same <error-page> tag i.e; which is either an servlet or a jsp. That servlet or the jsp acts the Error/Exception Handler in the end.

Consider the below example. It should be clear. Any type of exception thrown, fwd it to ErrorHandler.

<error-page>
   <exception-type>java.lang.Throwable</exception-type >
   <location>/ErrorHandler</location>
</error-page>

Instead of java.lang.Throwable, you can have a specific exception type like IOException or NullPointerException etc. 

You would have to use the error-page element in web.xml to specify the invocation of servlets in response to certain exceptions or HTTP status codes.

Similar to the exception type, we have error status codes as well.

<error-page>
   <error-code>404</error-code>
   <location>/ErrorHandler</location>
</error-page>

<error-page>
   <error-code>403</error-code>
   <location>/ErrorHandler</location>
</error-page>

In the above 2 examples for handling errors in the form of exceptions and error codes, the control goes to the location which is /ErrorHandler.

So the ErrorHandler here is an servlet which handles any type of exception as well as error codes 403 and 404 as seen above.


You can define different Error Handlers to handle different type of errors or exceptions. Above example is very much generic and hope it serve the purpose to explain you the basic concept.

Lets look at the implementation of the servlet which handles the exceptions.


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class ErrorHandler extends HttpServlet {
 
   // Handling GET method request.
   public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException { //line 2
         
      // Fetching the exception details from the request object.       
      Throwable throwable = (Throwable)
      request.getAttribute("javax.servlet.error.exception"); //line 3
      Integer statusCode = (Integer)
      request.getAttribute("javax.servlet.error.status_code");//line 4
      
      // Set response content type
      response.setContentType("text/html"); //line 5

      PrintWriter out = response.getWriter();
      String title = "Error/Exception Details";
      String htmlToSend =
         "<!doctype html public \"-//w3c//dtd html 4.0 " +
         "transitional//en\">\n";
         
      out.println(htmlToSend +
         "<html>\n" +
         "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor = \"#f0f0f0\">\n");

      if (throwable == null && statusCode == null) {
         out.println("<h2>Exception/Error info is missing</h2>");
      } else if (statusCode != null) {
         out.println("The status code fetched is : " + statusCode);
      } else {
         out.println("<h2>Exception/Error Info : </h2>");
         out.println("Exception Type : " + throwable.getClass( ).getName( ) + "</br>");
         out.println("The exception message: " + throwable.getMessage( ));
      }
      out.println("</body>");
      out.println("</html>");
   }
 
   public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      
      doGet(request, response);
   }
}

As seen above in line 3 and 4, the HttpServletRequest object has the attributes which contains the exception details.

The servlet writes those exception/error details in a html and sends the response to the user.

Below is the list of all the attributes with the descriptions:

javax.servlet.error.status_code :> It returns the error code.

javax.servlet.error.exception_type : > It returns the type of the exception which occured

javax.servlet.error.message :> It gives the complete error message.

javax.servlet.error.request_uri :> It returns the request uri.

javax.servlet.error.exception :> It returns the exception details.

javax.servlet.error.servlet_name :> It returns the servlet name.