HttpServlet的转发和重定向
Posted ssskkk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpServlet的转发和重定向相关的知识,希望对你有一定的参考价值。
一:重定向
1.HTTP协议规定了一种重定向的机制,重定向的运作流程如下
1.1 用户在浏览器输入特定的URL,请求访问服务端的某个组件。
1.2 服务端的组件返回一个状态码为302的响应结果。该响应结果的含义为:让浏览器在请求访问另一个Web组件。在响应结果中 提供了另一个组件的URL。
1.3 当浏览器端接收到这种响应结果后,再立即自动请求访问另一个Web组件
1.4 浏览器接收到来自另一个Web组件的响应结果
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
2.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实现的
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 viagetRequestDispatcher()
, theServletRequest
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 anIllegalStateException
. 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
orServletResponseWrapper
classes that wrap them.This method sets the dispatcher type of the given request to
DispatcherType.FORWARD
. - Parameters:
request
- aServletRequest
object that represents the request the client makes of the servletresponse
- aServletResponse
object that represents the response the servlet returns to the client
Servlet(源组件) 先对客户请求做一些预处理的操作,然后把请求转发给其他Web组件(目标组件)来完成包括生成相应结果在内的后序操作
转发的时 源组件和目标组件共享一个ServletRequest对象和ServletResponse对象(不同于重定向,转发的时候浏览器并没有重新像服务器发送一个请求)
这是转发和重定向最大的区别
补充:RequestDispatcher除了有forward方法外 还有include()方法 和转发的共同点是 源组件和目标组件共享一个ServletResponse对象
把API拷贝了下来 大家自己看吧。。。。。。
包含:include()方法
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
orServletResponseWrapper
classes that wrap them.This method sets the dispatcher type of the given request to
DispatcherType.INCLUDE
. - Parameters:
request
- aServletRequest
object that contains the client‘s requestresponse
- aServletResponse
object that contains the servlet‘s response
以上是关于HttpServlet的转发和重定向的主要内容,如果未能解决你的问题,请参考以下文章