Servlet Exception Handling
When a Servlet throws an exception, the Web container searches the web.xml that uses the exception-type element for a configuration matching the type of exception thrown.
You must use the error-page element in web.xml to specify the corresponding Servlet to invoke for a particularexception or HTTP Status code .
web.xml Configuration
Suppose there is a ErrorHandler Servlet that is invoked when any defined exception or error occurs. The following is the entry to 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 general error handler for all exceptions, you should define the following error-page instead of defining a separate error-page element for each exception:
java.lang.Throwable
/ErrorHandler
The following are points to note about the web.xml exception handling above:
- 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 404 (Not Found) or 403 (Forbidden), the ErrorHandler Servlet will be invoked.
- If the Web application throws ServletException or IOException, then the Web container will invoke the ErrorHandler Servlet.
- You can define different error handlers to handle different types of errors or exceptions. The example above is very general; hopefully you can understand the basic concepts through it.
Request Attributes - Errors/Exceptions
The following is a list of request attributes that the error-handling Servlet can access 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 information about 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 invoked 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. |
Servlet Error Handler Example
The following is a Servlet example that will act as an error handler whenever any error or exception you have defined occurs.
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:
//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 for handling GET requests
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 = "Runoob Tutorial Error/Exception Information";
String docType = "\n";
out.println(docType +
"\n" +
"" + title + " \n" +
"\n");
out.println("Runoob Tutorial异常信息实例演示
");
if (throwable == null && statusCode == null){
out.println("错误信息丢失
");
out.println("请返回 主页。");
}else if (statusCode != null) {
out.println("错误代码 : " + statusCode);
}else{
out.println("错误信息
");
out.println("Servlet Name : " + servletName +
"");
out.println("异常类型 : " +
throwable.getClass( ).getName( ) +
"");
out.println("请求 URI: " + requestUri +
"
");
out.println("异常信息: " +
throwable.getMessage( ));
}
out.println("");
out.println("");
}
// Method to handle POST requests
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Compile in the usual way ErrorHandler.javaand put your class files in
Let's add the following configuration to the web.xml file to handle exceptions:
ErrorHandler
com.runoob.test.ErrorHandler
ErrorHandler
/TomcatTest/ErrorHandler
404
/TomcatTest/ErrorHandler
java.lang.Throwable
/ErrorHandler
Now, try using a Servlet that generates an exception, or enter a wrong URL, which will trigger the Web container to invoke the ErrorHandler Servlet and display the appropriate message. For example, if you enter a wrong URL (such as: http://localhost:8080/TomcatTest/UnKonwPage), it will display the following result:
