When a Servlet throws an exception, the Web container searches the **web.xml** file using the `exception-type` element for a configuration that matches the thrown exception type.
You must use the **error-page** element in `web.xml` to specify a Servlet to be invoked for a specific **exception** or HTTP **status code**.
Assume there is an _ErrorHandler_ Servlet that is called whenever any defined exception or error occurs. The following entries would be created in `web.xml`.
ErrorHandler
ErrorHandler
ErrorHandler
/ErrorHandler
404
/ErrorHandler
403
/ErrorHandler
javax.servlet.ServletException
/ErrorHandler
java.io.IOException
/ErrorHandler
If you want a generic error handler for all exceptions, you should define the following `error-page` instead of defining separate `error-page` elements for each exception:
java.lang.Throwable
/ErrorHandler
Here are the key points to note regarding the above `web.xml` exception handling:
* The Servlet `ErrorHandler` is defined in the same way as other Servlets and is configured in `web.xml`.
* If an error status code occurs, whether it's 404 (Not Found) or 403 (Forbidden), the `ErrorHandler` Servlet will be called.
* If the web application throws a _ServletException_ or _IOException_, the Web container will call the `ErrorHandler` Servlet.
* You can define different error handlers to handle different types of errors or exceptions. The above example is very generic, and it is hoped that you can understand the basic concepts through the example.
Here is the list of request attributes accessible to the error-handling Servlet, used to analyze the nature of the error/exception.
| No. | Attribute & Description |
| --- | --- |
| 1 | **javax.servlet.error.status_code** This attribute gives the status code, which can be stored and analyzed after being stored as a `java.lang.Integer` data type. |
| 2 | **javax.servlet.error.exception_type** This attribute gives information about the exception type, which can be stored and analyzed after being stored as a `java.lang.Class` data type. |
| 3 | **javax.servlet.error.message** This attribute gives the exact error message, which can be stored and analyzed after being stored as a `java.lang.String` data type. |
| 4 | **javax.servlet.error.request_uri** This attribute gives information about the URL that called the Servlet, which can be stored and analyzed after being stored as a `java.lang.String` data type. |
| 5 | **javax.servlet.error.exception** This attribute gives information about the exception that occurred, which can be stored and analyzed after being stored as a `java.lang.Throwable` data type. |
| 6 | **javax.servlet.error.servlet_name** This attribute gives the name of the Servlet, which can be stored and analyzed after being stored as a `java.lang.String` data type. |
Here is the Servlet example that will act as the error handler for any error or exception you define.
This example gives you a basic understanding of exception handling in Servlets. You can use the same concepts to write more complex exception-handling applications:
```java
// Import necessary java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
// Extend HttpServlet class
public class ErrorHandler extends HttpServlet {
// Method to handle GET method request.
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
Throwable throwable = (Throwable)
request.getAttribute("javax.servlet.error.exception");
Integer statusCode = (Integer)
request.getAttribute("javax.servlet.error.status_code");
String servletName = (String)
request.getAttribute("javax.servlet.error.servlet_name");
if (servletName == null){
servletName = "Unknown";
}
String requestUri = (String)
request.getAttribute("javax.servlet.error.request_uri");
if (requestUri == null){
requestUri = "Unknown";
}
// Set response content type
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String title = "Tutorial Error/Exception Information";
String docType = "n";
out.println(docType +
"n" +
"
" + title + "n" +
"n");
out.println("
Tutorial Exception Information Example
");
if (throwable == null && statusCode == null){
out.println("
Error information is missing
");
out.println("Please return to the
home page.");
}else if (statusCode != null) {
out.println("Error code : " + statusCode);
}else{
out.println("
Error information
");
out.println("Servlet Name : " + servletName +
"");
out.println("Exception Type : " +
throwable.getClass( ).getName( ) +
"");
out.println("Request URI: " + requestUri +
"
");
out.println("Exception Message: " +
throwable.getMessage( ));
}
out.println("");
out.println("");
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Compile **ErrorHandler.java** in the usual way and place your class files in `/webapps/ROOT/WEB-INF/classes`.
Let us add the following configuration in `web.xml` file to handle exceptions:
```xml
ErrorHandler
com.tutorial.test.ErrorHandler
ErrorHandler
/TomcatTest/ErrorHandler
404
/TomcatTest/ErrorHandler
java.lang.Throwable
/ErrorHandler
Now, try to use a Servlet that generates an exception, or enter a wrong URL. This will trigger the Web container to call the **ErrorHandler** Servlet and display an appropriate message. For example, if you enter a wrong URL (like: http://localhost:8080/TomcatTest/UnKonwPage), it will display the following result:
!(#)