什么是servlet处理中的“错误调度”? [英] What is an 'error dispatch' in servlet processing?

查看:282
本文介绍了什么是servlet处理中的“错误调度”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javax.servlet.AsyncContext接口的 javadoc 说:

The javadoc of the javax.servlet.AsyncContext interface says:


如果异步操作超时,
容器必须运行以下步骤:

In the event that an asynchronous operation has timed out, the container must run through these steps:


  • onTimeout 方法中调用所有 AsyncListener 使用 ServletRequest 注册的实例,其中启动了异步

    操作。

  • 如果没有一个侦听器调用 complete()或任何 dispatch()方法,执行错误调度,状态代码
    等于 HttpServletResponse.SC_INTERNAL_SERVER_ERROR

  • 如果未找到匹配的错误页面,或错误页面未调用 complete()或任何 dispatch()方法,调用 complete()

  • 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 惊呼了相同。 (用他们确切的话说:我也没有看到'错误发送'的定义',

But I couldn't find the meaning of "error dispatch" anywhere. In fact there was an Apache bug that exclaimed the same. (In their exact words: "I haven't seen the def. of 'error dispatch', too")

但当然,必须有明确的定义这个以及如何使用它。
有谁知道?

But of course, there must be a clear definition for this and how to use it. Does anyone know?

推荐答案

在异常/错误期间由容器进行的调度称为错误调度。这些通常是调度到错误页面。我无法直接执行错误调度。

Dispatches made by the container during exceptions/errors are called error dispatches. These are usually dispatches to error pages. There is no way to directly do an error dispatch as I know it.

通过错误调度发出的请求将调度程序类型设置为DispatcherType.ERROR。 (在servlet的服务方法代码中,您可以使用getDispatcherType()获取调度类型)

A request that has come through an error dispatch will have dispatcher type set to DispatcherType.ERROR. (In the servlet's service method code, you can get the dispatch type using getDispatcherType())

以下六个请求范围属性也将在错误调度中设置。

The following six request scoped attributes will also be set in error dispatches.

"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"

因此,如果您有容器重定向错误的错误页面,您知道可以阅读这六个属性以获取更多信息。

So if you have an error page to which the container redirects errors, you know you can read those six attributes for more information.

http://docs.oracle.com/javaee/6/api/javax/servlet/DispatcherType.html
http://docs.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html

您可以使用部署描述符(web.xml)中的标记来设置错误分派。例如,如果您为404错误代码添加了错误页面标记,那么当找不到页面错误时,容器将调度到该页面。在该错误页面中,您可以使用request.getAttribute(javax.servlet.error.message)之类的代码来检索有关错误的详细信息。示例...

You can setup an error dispatch by using tag in deployment descriptor (web.xml). For example if you added an error-page tag for 404 error code, then the container will dispatch to that page when a page not found error occurs. In that error page, you can use code like request.getAttribute("javax.servlet.error.message") to retrieve details about the error. Example ...

web.xml:

<web-app>
    <error-page>
        <error-code>404</error-code>
        <location>/error.jsp</location>
    </error-page>
</web-app>

error.jsp:

error.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进行错误调度。

In the above sample application, if a client requested page is not found or you use response.sendError("404", "...") somewhere, the container will do an error dispatch to error.jsp.

JSP错误处理机制(使用errorPage和isErrorPage页面指令)也适用于此。

The JSP error handling mechanism (using "errorPage" and "isErrorPage" page directives) also applies here.

这篇关于什么是servlet处理中的“错误调度”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆