Servlets 简明教程
Servlets - Exception Handling
当 servlet 引发异常时,Web 容器会搜索 web.xml 中使用 exception-type 元素匹配引发异常类型的配置。
When 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.
您需要在 web.xml 中使用 error-page 元素,以指定根据特定 exceptions 或 HTTP status codes 来调用 servlet。
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.
web.xml Configuration
假设您有一个 ErrorHandler servlet,它将在出现任何已定义异常或错误时被调用。下面是在 web.xml 中创建的条目。
Consider, you have an ErrorHandler servlet which would be called whenever there is any defined exception or error. Following would be the entry created in web.xml.
<!-- servlet definition -->
<servlet>
<servlet-name>ErrorHandler</servlet-name>
<servlet-class>ErrorHandler</servlet-class>
</servlet>
<!-- servlet mappings -->
<servlet-mapping>
<servlet-name>ErrorHandler</servlet-name>
<url-pattern>/ErrorHandler</url-pattern>
</servlet-mapping>
<!-- error-code related error pages -->
<error-page>
<error-code>404</error-code>
<location>/ErrorHandler</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/ErrorHandler</location>
</error-page>
<!-- exception-type related error pages -->
<error-page>
<exception-type>
javax.servlet.ServletException
</exception-type >
<location>/ErrorHandler</location>
</error-page>
<error-page>
<exception-type>java.io.IOException</exception-type >
<location>/ErrorHandler</location>
</error-page>
如果您想针对所有异常使用通用错误处理程序,则应该定义以下 error-page,而不是针对每个异常定义单独的 error-page 元素 −
If you want to have a generic Error Handler for all the exceptions then you should define following error-page instead of defining separate error-page elements for every exception −
<error-page>
<exception-type>java.lang.Throwable</exception-type >
<location>/ErrorHandler</location>
</error-page>
以下是在 Exception Handling 的 web.xml 中要注意的要点 −
Following are the points to be noted about above web.xml for Exception Handling −
-
The servlet ErrorHandler is defined in usual way as any other servlet and configured in web.xml.
-
If there is any error with status code either 404 (Not Found) or 403 (Forbidden ), then ErrorHandler servlet would be called.
-
If the web application throws either ServletException or IOException, then the web container invokes the /ErrorHandler servlet.
-
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.
Request Attributes − Errors/Exceptions
以下是错误处理 servlet 可访问的请求属性列表,用于分析错误/异常的本质。
Following is the list of request attributes that an error-handling servlet can access to analyze the nature of error/exception.
Sr.No. |
Attribute & Description |
1 |
*javax.servlet.error.status_code * This attribute give status code which can be stored and analyzed after storing in a java.lang.Integer data type. |
2 |
*javax.servlet.error.exception_type * This attribute gives information about exception type which can be stored and analysed after storing in a java.lang.Class data type. |
3 |
*javax.servlet.error.message * This attribute gives information exact error message which can be stored and analyzed after storing in a java.lang.String data type. |
4 |
*javax.servlet.error.request_uri * This attribute gives information about URL calling the servlet and it can be stored and analysed after storing in a java.lang.String data type. |
5 |
*javax.servlet.error.exception * This attribute gives information about the exception raised, which can be stored and analysed. |
6 |
*javax.servlet.error.servlet_name * This attribute gives servlet name which can be stored and analyzed after storing in a java.lang.String data type. |
Error Handler Servlet Example
此示例将使您基本了解 Servlet 中的异常处理,但是您可以使用相同概念编写更复杂的筛选器应用程序−
This example would give you basic understanding of Exception Handling in Servlet, but you can write more sophisticated filter applications using the same concept −
// Import required 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 {
// Analyze the servlet exception
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");
PrintWriter out = response.getWriter();
String title = "Error/Exception Information";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n");
if (throwable == null && statusCode == null) {
out.println("<h2>Error information is missing</h2>");
out.println("Please return to the <a href=\"" +
response.encodeURL("http://localhost:8080/") +
"\">Home Page</a>.");
} else if (statusCode != null) {
out.println("The status code : " + statusCode);
} else {
out.println("<h2>Error information</h2>");
out.println("Servlet Name : " + servletName + "</br></br>");
out.println("Exception Type : " + throwable.getClass( ).getName( ) + "</br></br>");
out.println("The request URI: " + requestUri + "<br><br>");
out.println("The exception message: " + throwable.getMessage( ));
}
out.println("</body>");
out.println("</html>");
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
使用通常方法编译 ErrorHandler.java ,然后将您的类文件放入/webapps/ROOT/WEB-INF/classes 中。
Compile ErrorHandler.java in usual way and put your class file in /webapps/ROOT/WEB-INF/classes.
让我们在 web.xml 中添加以下配置来处理异常 −
Let us add the following configuration in web.xml to handle exceptions −
<servlet>
<servlet-name>ErrorHandler</servlet-name>
<servlet-class>ErrorHandler</servlet-class>
</servlet>
<!-- servlet mappings -->
<servlet-mapping>
<servlet-name>ErrorHandler</servlet-name>
<url-pattern>/ErrorHandler</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/ErrorHandler</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type >
<location>/ErrorHandler</location>
</error-page>
现在尝试使用引发任何异常或键入错误 URL 的 servlet,这将触发 Web 容器调用 ErrorHandler servlet 并显示适当的编程信息。例如,如果您键入一个错误的 URL,则会显示以下结果 −
Now try to use a servlet which raise any exception or type a wrong URL, this would trigger Web Container to call ErrorHandler servlet and display an appropriate message as programmed. For example, if you type a wrong URL then it would display the following result −
The status code : 404
上述代码可能无法与某些 Web 浏览器配合使用。因此,请尝试使用 Mozilla 和 Safari,它应该可以正常工作。
The above code may not work with some web browsers. So try with Mozilla and Safari and it should work.