What is an 'error dispatch' in servlet processing?

一笑奈何 提交于 2019-12-01 06:36:59

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!