java - 什么是servlet处理中的 'error dispatch'?

标签 java jakarta-ee servlets servlet-3.0 servletexception

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, all AsyncListener instances registered with the ServletRequest on which the asynchronous
    operation was initiated.
  • If none of the listeners called complete() or any of the dispatch() methods, perform an error dispatch with a status code equal to HttpServletResponse.SC_INTERNAL_SERVER_ERROR.
  • If no matching error page was found, or the error page did not call complete() or any of the dispatch() methods, call complete().

但是我在任何地方都找不到“错误调度”的含义。 事实上有一个 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/

相关文章:

html - HttpServletRequest JSP 的多个复选框

java - servlet url 模式匹配实现错误

java - IBM Notes Java 脚本库或包

java - 如何让我的带有音频的 Java 应用程序在 Linux 中播放得很好?

java - 为什么log4j无法生成备份文件?

java - 尝试通过 html 添加基本的富文本支持到 jface tableviewer?

java - 在eclipse中调试Jar文件

rest - 在 JAX RS 中,返回 Response 和 Bean 或 Collection of Beans (DTO) 之间的区别

java - 如何使用 Vaadin Flow CallBackDataProvider 修复 'Incompatible Types' 错误

java - 如何缩短JSP中的数字?