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 ...
随机推荐
- exl表格找两个字符间的数据
例子找的是]XXX,中间的内容 =MID(B2,FIND("]",B2)+1,FIND(",",B2)-FIND("]",B2)-1) ...
- python 将mysql数据库中的int类型修改为NULL 报1366错误,解决办法
gt.run_sql()是用pymysql 封装的类 distribution_sort_id type: int目的:将此字段值全部修改为NULL g=2gt.run_sql("updat ...
- VUE(相关简介及初始)
1.什么是vue Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的 渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注视图层, ...
- Jenkins入门,介绍、安装
一.介绍 Jenkins: 三.安装 Jenkins: 第一种方式: 下载 windows 版本:进入 Jenkins 官网:https://jenkins.io/ 进入下 ...
- java37
1.键盘录入格式: Scanner sc = new Scanner(System.in); int i = sc.nextInt();//录入整数 字符: Scanner sc1 = new Sca ...
- rem 原理与简介
一.rem 原理与简介 字体单位: 值根据 html 根元素大小而定,同样可以作为宽度,高度等单位 适配原理: 将 px 替换为 rem,动态修改 HTML 根元素的 font-siz ...
- 《Linux就该这么学》第十九天课程
今天对“Linux就该这么学”课程做个收尾 最后一张使用LNMP架构部署动态网站环境 第1步:下载及解压源码包文件.为了方便在网络中传输,源码包文件通常会在归档后使用gzip或bzip2等格式进行压缩 ...
- ios 国际化开发
一,.xib 1.首先选中xib文件,在右边的inspector中选择对应的国际化语言,如下图
- java策略设计模式
1.概述 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使他们可以相互替换,让算法独立于使用它的客户而独立变化. 其实不要被晦涩难懂的定义所迷惑,策略设计模式实际上就是定义一个接口,只要实现 ...
- Github使用: 本地上传, 与之同步
很久不用Github 又忘记了怎么同步了, Git桌面前年已经部署好了. 1. 打开GitHub Desktop -- file -- clone repository --- 操作其中想要的一个文件 ...