HttpServletResponse重定向

1.HTTP协议规定了一种重定向的机制,重定向的运作流程如下

 用户在浏览器输入特定的URL,请求访问服务端的某个组件。
 服务端的组件返回一个状态码为302的响应结果。该响应结果的含义为:让浏览器在请求访问另一个Web组件。在响应结果中 提供了另一个组件的URL。
 当浏览器端接收到这种响应结果后,再立即自动请求访问另一个Web组件
 浏览器接收到来自另一个Web组件的响应结果

补充301重定向和302重定向的区别

  301重定向是永久的重定向,不会保留旧的网址,搜索引擎抓取内容的同时会将新的网址替换旧的网址。适用于 网站的跳转

  302重定向是暂时的重定向,搜索引擎抓取新的内容时会保留旧的地址,适用于未登录跳转到登录页面

2. 在JAVA Servlet API中 用于重定向的HttpServletResponse 接口

sendRedirect方法:

void sendRedirect(java.lang.String location)
                  throws java.io.IOException

Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer. The buffer will be replaced with the data set by this method. Calling this method sets the status code to SC_FOUND 302 (Found). This method can accept relative URLs;the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading ‘/’ the container interprets it as relative to the current request URI. If the location is relative with a leading ‘/’ the container interprets it as relative to the servlet container root.

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

Parameters:location – the redirect location URL
 注意是 HttpServletResponse,ServletResponse中是没有sendRedirect方法的。因为重定向机制是HTTP协议规定的。
 API中蓝色字体,表示response.sendRedirect(String location),如果参数location 以”/”开头 表示相对URL 。如果以”Http://”开头 表示绝对路径
 API字体中的金色字体,表示 如果在源组件进行重定向之前,已经提交了响应结果(调用ServletResponse相关的close()方法),那么sendRedirect方法会抛出IllegalStateException异常
API中的紫色字体,表示 Servlet源组件生成的响应结果 不会被发送到客户端,sendRedirect一律返回302 浏览器接收到302状态码的时候 会立刻请求目标组件的URL。客户端接到的响应结果是目标Web组件的响应结果
 源组件和目标组件不共享一个ServletRequest对象,因此不共享请求范围内的共享数据(显而易见 是浏览器重新像服务器发送了一个请求)

RequestDispatcher转发

转发是通过RequestDispatcher实现的

RequestDispatcher:请求分发器(故名思议: 同一个请求 分发不同的Servlet)

首先看实现转发的:forward方法

void forward(ServletRequest request,
             ServletResponse response)
             throws ServletException,
                    java.io.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.

This method sets the dispatcher type of the given request to DispatcherType.FORWARD.

Parameters:request – a ServletRequest object that represents the request the client makes of the servletresponse – a ServletResponse object that represents the response the servlet returns to the client 
Servlet(源组件) 先对客户请求做一些预处理的操作,然后把请求转发给其他Web组件(目标组件)来完成包括生成相应结果在内的后序操作
转发的时 源组件和目标组件共享一个ServletRequest对象和ServletResponse对象(不同于重定向,转发的时候浏览器并没有重新像服务器发送一个请求)

这是转发和重定向最大的区别

补充:RequestDispatcher除了有forward方法外 还有include()方法 include()和forward()的共同点是 源组件和目标组件共享一个ServletRequest对象

void include(ServletRequest request,
             ServletResponse response)
             throws ServletException,
                    java.io.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.

This method sets the dispatcher type of the given request to DispatcherType.INCLUDE.

Parameters:request – a ServletRequest object that contains the client’s requestresponse – a ServletResponse object that contains the servlet’s response