HttpServlet的转发和重定向
HttpServletResponse重定向
1.HTTP协议规定了一种重定向的机制,重定向的运作流程如下
- 用户在浏览器输入特定的URL,请求访问服务端的某个组件。
- 服务端的组件返回一个状态码为302的响应结果。该响应结果的含义为:让浏览器在请求访问另一个Web组件。在响应结果中 提供了另一个组件的URL。
- 当浏览器端接收到这种响应结果后,再立即自动请求访问另一个Web组件
- 浏览器接收到来自另一个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_FOUND302 (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
RequestDispatcherobtained viagetRequestDispatcher(), theServletRequestobject has its path elements and parameters adjusted to match the path of the target resource.forwardshould 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
ServletRequestWrapperorServletResponseWrapperclasses that wrap them.This method sets the dispatcher type of the given request to
DispatcherType.FORWARD. -
- Parameters:
request- aServletRequestobject that represents the request the client makes of the servletresponse- aServletResponseobject that represents the response the servlet returns to the client
- Servlet(源组件) 先对客户请求做一些预处理的操作,然后把请求转发给其他Web组件(目标组件)来完成包括生成相应结果在内的后序操作
- 转发的时 源组件和目标组件共享一个ServletRequest对象和ServletResponse对象(不同于重定向,转发的时候浏览器并没有重新像服务器发送一个请求)
这是转发和重定向最大的区别
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
ServletResponseobject 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
ServletRequestWrapperorServletResponseWrapperclasses that wrap them.This method sets the dispatcher type of the given request to
DispatcherType.INCLUDE. -
- Parameters:
request- aServletRequestobject that contains the client's requestresponse- aServletResponseobject that contains the servlet's response
HttpServlet的转发和重定向的更多相关文章
- Servlet中转发和重定向的区别
Servlet中页面的跳转有两种方式:转发和重定向. 1.转发和重定向的区别 ①转发是服务器行为,重定向是客户端行为. ②转发是浏览器发生了一次请求,重定向至少是两次请求. ③转发地址栏中的url不会 ...
- JavaWeb学习之转发和重定向、会话技术:cookie、session、验证码实例、URLConnection使用(下载网页)(4)
1.转发和重定向 HttpServletResponse response 转发: RequestDispatcher dispatcher = request.getRequestDispatche ...
- jsp:和属性相关的方法,请求的转发,重定向
jsp中与属性相关的方法: 方法: void setAttribute(String name, Object o): 设置属性 Object getAttribute(String name):获取 ...
- 定向转发和重定向实现 <select >下拉表单数据传送
定向转发的特点: (1). 实行转发时浏览器上的网址不变 (如果你这点忽视了,那你就要接受我无尽的鄙视吧! 哇咔咔~~~) (2). 实行转发时 : 只有一次请求. 不信,看这下面的 ...
- javaWEB总结(14):请求的转发和重定向
通俗理解请求转发与重定向的流程 通俗的来讲:我们可以把请求转发和重定向当成是一种借钱的情景模式. (1)请求的转发:A向B借钱,B自己没有钱,但是向C借到了钱,并且把钱借给了A.A只向B请求了一次. ...
- HttpServletRequest 接口、HttpServletResponse 接口、请求转发与重定向
上篇文章我们讲了servlet的基本原理,这章将讲一下剩余的部分. HttpServletRequest 接口 该接口是 ServletRequest 接口的子接口,封装了 HTTP 请求的相关信息, ...
- HttpRequest,HttpResponse,乱码,转发和重定向
HttpServletRequest简介 Web服务器收到客户端的http请求,会针对每一次请求,创建一个用于代表请求的HttpServletRequest类型的request对象,并将"H ...
- Servlet(四):转发与重定向、路径问题
在上次的小案例中用到了转发的技术,今天来仔细聊聊转发和重定向的问题,以及一些小知识的汇总. 一.转发 1.转发的概念 转发主要是将浏览器的请求交给另外一个servlet或jsp来处理,借助reques ...
- 04_web基础(六)之请求转发与重定向
1.交互方式 Web组件之间跳转: 从AServlet 跳转到 BServlet. 三种类型: 1:请求转发(forward) 2:URL重定向(redirect) 3:请求包含(include) 3 ...
随机推荐
- $.isPlainObject
// Support: IE<9 // Handle iteration over inherited properties before own properties. if ( !suppo ...
- position在IE下的问题
盒子设置position:absolute;后,子盒子的浮动可以不做清除浮动处理,同样,使用fixed,子盒子也不需要做清除浮动的处理 在IE7下(据说IE6也不行),盒子设置overflow:hid ...
- vuex状态管理
msvue组件间通信时,若需要改变多组件间共用状态的值.通过简单的组件间传值就会遇到问题.如:子组件只能接收但改变不了父组件的值.由此,vuex的出现就是用作各组件间的状态管理. 简单实例:vuex的 ...
- 20175325 《JAVA程序设计》实验二《JAVA开发环境的熟悉》实验报告
20175325 <JAVA程序设计>实验二<JAVA开发环境的熟悉>实验报告 一.实验报告封面 课程:Java程序设计 班级:1753班 姓名:石淦铭 学号:20175325 ...
- 探索未知种族之osg类生物---状态树与渲染树以及节点树之间的关系
节点树 首先我们来看一个场景构建的实例,并通过它来了解一下“状态节点”StateGraph 和“渲染叶”RenderLeaf 所构成的状态树,“渲染台”RenderStage 和“渲染元”Render ...
- GUI学习之八——复选框QCheckBox的学习总结
一.描述 a.QCheckBox一般用于给用户提供若干选项中多个选择时的使用 b.控件左侧有一个方框来显示控件被选中. c.复选框是有三种状态的 二.使用 1.创建 复选框的创建和常规的按钮创建方式是 ...
- 腾讯开源的 Paxos库 PhxPaxos 代码解读---Accept阶段(一)
腾讯开源的 Paxos库 PhxPaxos 代码解读---Accept阶段(一) 在看Accept阶段代码之前, 我们再回想一下 Basic Paxos算法; 1. Basic Paxos 算法是为 ...
- jquery倒计时按钮常用于验证码倒计时
<!doctype html><html><head> <meta charset="utf-8"> <title>jq ...
- Android学习(二)
学号 20189214 <Android程序设计>第七周学习总结 教材学习内容总结 监听 设置点击监听的5种方式 方法1:直接用匿名内部类 这是最常用的一种方法,直接setXXXListe ...
- 从集合中查找最值得方法——max(),min(),nlargest(),nsmallest()
从集合中查找最值得方法有很多,常用的方法有max(),min(),nlargest(),nsmallest()等. 一.max()和min() 1.1 入门用法 直接使用max(),min(),返回可 ...