RequestDispatcher (Java EE 5)

Java EE API


javax.servlet Interface RequestDispatcher


public interface RequestDispatcher


定义接收来自客户端的请求并将它们发送到服务器上的任何资源(比如 servlet、HTML 文件或 JSP 文件)的对象。servlet 容器可创建 RequestDispatcher 对象,该对象被用作包装位于特定路径上的服务器资源或通过特定名称给定的服务器资源的包装器。

此接口用于包装 servlet,但 servlet 容器可创建包装任何类型资源的 RequestDispatcher 对象。

英文文档:

Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or given by a particular name.

This interface is intended to wrap servlets, but a servlet container can create RequestDispatcher objects to wrap any type of resource.

Author:
Various
See Also:
ServletContext.getRequestDispatcher(java.lang.String), ServletContext.getNamedDispatcher(java.lang.String), ServletRequest.getRequestDispatcher(java.lang.String)

Method Summary
 void
 void
 

Method Detail

public void forward(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException
将请求从一个 servlet 转发到服务器上的另一个资源(servlet、JSP 文件或 HTML 文件)。此方法允许一个 servlet 对请求进行初步处理,并使另一个资源生成响应。

对于通过 getRequestDispatcher() 获得的 RequestDispatcherServletRequest 对象使其路径元素和参数进行调整以匹配目标资源的路径。

在将响应提交到客户端之前(在刷新响应正文输出之前),应该调用 forward。如果已经提交了响应,则此方法抛出 IllegalStateException。在转发之前,自动清除响应缓冲区中未提交的输出。

request 和 response 参数必须是传入调用的 servlet service 方法的对象,或者是包装它们的 ServletRequestWrapperServletResponseWrapper 类的子类。

request 表示客户端对 servlet 发出的请求的 ServletRequest 对象
response 表示 servlet 向客户端返回的响应的 ServletResponse 对象
ThrowsServletException: 如果目标资源抛出此异常
Throwsjava.io.IOException: 如果目标资源抛出此异常
ThrowsIllegalStateException: 如果已提交响应

英文文档:

forward

void forward(ServletRequest request,
             ServletResponse response)
             throws ServletException,
                    IOException
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.

For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and parameters adjusted to match the path of the target resource.

forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.

The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the ServletRequestWrapper or ServletResponseWrapper classes that wrap them.

Parameters:
request - a ServletRequest object that represents the request the client makes of the servlet
response - a ServletResponse object that represents the response the servlet returns to the client
Throws:
ServletException - if the target resource throws this exception
IOException - if the target resource throws this exception
IllegalStateException - if the response was already committed

public void include(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException
包含响应中某个资源(servlet、JSP 页面和 HTML 文件)的内容。事实上,此方法支持编程式服务器端包含。

ServletResponse 对象使其路径元素和参数不根据调用者的路径元素和参数发生改变。包含的 servlet 无法更改响应状态代码或设置头;任何更改尝试都将被忽略。

request 和 response 参数必须是传入调用的 servlet service 方法的对象,或者是包装它们的 ServletRequestWrapperServletResponseWrapper 类的子类。

request 包含客户端请求的 ServletRequest 对象
response 包含 servlet 的响应的 ServletResponse 对象
ThrowsServletException: 如果包含的资源抛出此异常
Throwsjava.io.IOException: 如果包含的资源抛出此异常

英文文档:

include

void include(ServletRequest request,
             ServletResponse response)
             throws ServletException,
                    IOException
Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes.

The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.

The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the ServletRequestWrapper or ServletResponseWrapper classes that wrap them.

Parameters:
request - a ServletRequest object that contains the client's request
response - a ServletResponse object that contains the servlet's response
Throws:
ServletException - if the included resource throws this exception
IOException - if the included resource throws this exception


Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!