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:
- 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()
.
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.
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
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 :
<!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>
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.
The JSP error handling mechanism (using "errorPage" and "isErrorPage" page directives) also applies here.
Another information which is not directly related to the question but which I have seen not clearly mentioned in almost all places, including the answer above is that the error dispatch is technically NOT "redirect", it's rather internal forward. For beginners this might seem confusing as its usually written all over as "container does the redirect to the error page". So the moment your container sees an http error code / uncaught exception being sent by any of the resource it begins to look into the web xml to see if there are any handler defined and if present then it appropriately forwards the request to that resource using a request dispatcher. Then obviously all the information mentioned in the above answer applies.
If some one however wants to do an redirect, they can do so in filter or servlet or from anywhere they have response object available.
来源:https://stackoverflow.com/questions/16324114/what-is-an-error-dispatch-in-servlet-processing