RequestDispatcher、sendRedirect()

1.request.getRequestDispatcher()是请求转发,前后页面共享一个request ;
response.sendRedirect()是重新定向,前后页面不是一个request。

request.getRequestDispather();返回的是一个RequestDispatcher对象。

2.RequestDispatcher.forward()是在服务器端运行;
HttpServletResponse.sendRedirect()是通过向客户浏览器发送命令来完成.
所以RequestDispatcher.forward()对于浏览器来说是“透明的”;
而HttpServletResponse.sendRedirect()则不是。

HttpServletResponse.sendRedirect()方法将 响应定向到参数location指定的、新的URL。location可以是一个绝对的URL,如 response.sendRedirect("http://java.sun.com")也可以使用相对的URL。如果location以“/”开 头,则容器认为相对于当前Web应用的根,否则,容器将解析为相对于当前请求的URL。这种重定向的方法,将导致客户端浏览器的请求URL跳转。从浏览器 中的地址栏中可以看到新的URL地址,作用类似于上面设置HTTP响应头信息的实现。

RequestDispatcher.forward()方法将 当前的request和response重定向到该RequestDispacher指定的资源。这在实际项目中大量使用,因为完成一个业务操作往往需要 跨越多个步骤,每一步骤完成相应的处理后,转向到下一个步骤。比如,通常业务处理在Servlet中处理,处理的结果转向到一个JSP页面进行显示。这样 看起来类似于Servlet链的功能,但是还有一些区别。一个RequestDispatcher对象可以把请求发送到任意一个服务器资源,而不仅仅是另 外一个Servlet。 include()方法将把Request Dispatcher资源的输出包含到当前输出中。

 

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.

Method Summary

 

 void

forward(ServletRequest request, ServletResponse response)

          Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.

 void

include(ServletRequest request, ServletResponse response)

          Includes the content of a resource (servlet, JSP page, HTML file) in the response.

 

获得对象方法 使用request.:

 

RequestDispatcher

getRequestDispatcher(java.lang.String path)

          Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path

 

forward  原先请求用的什么方式 就用什么方式 get or post

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.

include

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.

 

区别:

forward方法是把请求的内容转发到另外的一个servlet.而include是把另一个servlet处理过后的内容拿过来.

举例来说比如在servlet1打一句out.print("1111"),servlet2打上out.print("22222"),在servlet1中用forward命令会转到servlet2中,显示22222.

而在servlet1中使用include方法会依然在servlet1的页面中,但是在1111后打出22222

 

例如:

 request.getRequestDispatcher("/index.jsp").forward(request, response);

显示:

 request.getRequestDispatcher("/index.jsp").include(request, response);

显示:

 

posted on 2013-09-02 15:27  TrustNature  阅读(337)  评论(0编辑  收藏  举报