标签:UNC file 输入 ref 完成 的区别 接口 should rap
一:重定向
1.HTTP协议规定了一种重定向的机制,重定向的运作流程如下
1.1 用户在浏览器输入特定的URL,请求访问服务端的某个组件。
1.2 服务端的组件返回一个状态码为302的响应结果。该响应结果的含义为:让浏览器在请求访问另一个Web组件。在响应结果中 提供了另一个组件的URL。
1.3 当浏览器端接收到这种响应结果后,再立即自动请求访问另一个Web组件
1.4 浏览器接收到来自另一个Web组件的响应结果
2. 在JAVA Servlet API中 用于重定向的HttpServletResponse 接口
void sendRedirect(java.lang.String location) throws java.io.IOException
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.
location
- the redirect location URL2.1 注意是 HttpServletResponse,在ServletResponse中是没有sendRedirect方法的。因为重定向机制是HTTP协议规定的。
2.2 在Servlet源组件中调用 response.sendRedirect()方法之后的代码块也会被执行
2.3 API中蓝色字体,表示response.sendRedirect(String location),如果参数location 以"/"开头 表示相对URL 。如果以"Http://"开头 表示绝对路径
2.4 API字体中的金色字体,表示 如果在源组件进行重定向之前,已经提交了响应结果(调用ServletResponse相关的close()方法),那么sendRedirect方法会抛出IllegalStateException异常
2.5 API中的紫色字体,表示 Servlet源组件生成的响应结果 不会被发送到客户端,sendRedirect一律返回302 浏览器接收到302状态码的时候 会立刻请求目标组件的URL。客户端接到的响应结果是目标Web组件的响应结果
2.6 源组件和目标组件不共享一个ServletRequest对象,因此不共享请求范围内的共享数据(显而易见 是浏览器重新像服务器发送了一个请求)
二:转发
转发和包含都是通过RequestDispatcher实现的
首先看转发:forward方法
void forward(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException
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
.
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()方法 和转发的共同点是 源组件和目标组件共享一个ServletResponse对象
把API拷贝了下来 大家自己看吧。。。。。。
void include(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException
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
.
request
- a ServletRequest
object that contains the client‘s requestresponse
- a ServletResponse
object that contains the servlet‘s response
标签:UNC file 输入 ref 完成 的区别 接口 should rap
原文地址:https://www.cnblogs.com/ssskkk/p/9194163.html