javadoc of the javax.servlet.AsyncContext interface说:
In the event that an asynchronous operation has timed out, the container must run through these steps:
- Invoke, at their
onTimeout
method, allAsyncListener
instances registered with theServletRequest
on which the asynchronous
operation was initiated.- If none of the listeners called
complete()
or any of thedispatch()
methods, perform an error dispatch with a status code equal toHttpServletResponse.SC_INTERNAL_SERVER_ERROR
.- If no matching error page was found, or the error page did not call
complete()
or any of thedispatch()
methods, callcomplete()
.
但是我在任何地方都找不到“错误调度”的含义。 事实上有一个 Apache bug那同样惊呼。 (用他们原话来说:“我也没有看到‘error dispatch’的定义”)
但是当然,对于这个以及如何使用它必须有一个明确的定义。 有人知道吗?
最佳答案
容器在异常/错误期间进行的分派(dispatch)称为错误分派(dispatch)。这些通常是发送到错误页面。据我所知,没有办法直接进行错误调度。
通过错误分派(dispatch)发出的请求会将分派(dispatch)器类型设置为 DispatcherType.ERROR。 (在servlet的服务方法代码中,可以使用getDispatcherType()获取调度类型)
以下六个请求范围的属性也将在错误调度中设置。
"javax.servlet.error.exception"
"javax.servlet.error.exception_type"
"javax.servlet.error.message"
"javax.servlet.error.request_uri"
"javax.servlet.error.servlet_name"
"javax.servlet.error.status_code"
因此,如果您有一个容器将错误重定向到的错误页面,您就知道您可以阅读这六个属性以获取更多信息。
http://docs.oracle.com/javaee/6/api/javax/servlet/DispatcherType.html http://docs.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html
您可以使用部署描述符 (web.xml) 中的标记来设置错误分派(dispatch)。例如,如果您为 404 错误代码添加了一个错误页面标签,那么当页面未找到错误发生时,容器将分派(dispatch)到该页面。在该错误页面中,您可以使用 request.getAttribute("javax.servlet.error.message") 之类的代码来检索有关错误的详细信息。示例 ...
网络.xml :
<web-app>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
错误.jsp :
<!DOCTYPE html>
<html>
<head>
<title>404 Error</title>
</head>
<body>
The page was not found. You requested <%= request.getAttribute("javax.servlet.error.message") %> but it was not found. Sorry.
</body>
</html>
在上面的示例应用程序中,如果未找到客户端请求的页面或者您在某处使用了 response.sendError("404", "..."),容器将向 error.jsp 发送错误消息。
JSP 错误处理机制(使用“errorPage”和“isErrorPage”页面指令)也适用于此。
关于java - 什么是servlet处理中的 'error dispatch'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16324114/