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_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 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对象(不同于重定向,转发的时候浏览器并没有重新像服务器发送一个请求)
这是转发和重定向最大的区别
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的转发和重定向的更多相关文章
- 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 ...
随机推荐
- 爬虫利器 Puppeteer 实战
Puppeteer 介绍 Puppeteer 翻译是操纵木偶的人,利用这个工具,我们能做一个操纵页面的人.Puppeteer是一个Nodejs的库,支持调用Chrome的API来操纵Web,相比较Se ...
- Convolutional LSTM Network: A Machine LearningApproach for Precipitation Nowcasting
Convolutional LSTM Network: A Machine LearningApproach for Precipitation Nowcasting 这篇文章主要是了解方法. 原始文 ...
- 安卓学习第一节--环境搭建及Android Studio 安装
1.安装JDK 2.安装AS 安装参考网址 https://www.cnblogs.com/xiadewang/p/7820377.html 下载网址: http://www.android-stud ...
- 南昌邀请赛I.Max answer 单调栈+线段树
题目链接:https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value of a in ...
- Navicat Premium 出现2059错误解决办法
1,登陆后可查询默认加密规则,键入 use mysql; select user,plugin from user where user ='root'; 解决办法: 1,更新用户密码:ALTER U ...
- [总结]给pcDuino v2编译Linux kernel
1.版本问题 推荐选择pcdunio提供的官方的kernel. 当然可以选用www.github.com/linux-sunxi 中的kernel,不过有很多驱动都用不了包括arduino. 我尝试了 ...
- MySQL 连接出现 Authentication plugin 'caching_sha2_password' cannot be loaded
参考帖子: https://www.cnblogs.com/zhurong/p/9898675.html cmd 需要使用管理员权限打开
- CENTOS7上安装MYSQL5.7.21流程
1系统约定安装文件下载目录:/data/softwareMysql目录安装位置:/usr/local/mysql数据库保存位置:/data/mysql日志保存位置:/data/log/mysql 2下 ...
- 使用signalr实现网页和微信公众号实时聊天(上)
最近项目中需要实现客户在公众号中和客服(客服使用后台网站系统)进行实时聊天的功能.折腾了一段时间,实现了这个功能.现在将过程记录下,以便有相同需求的同行可以参考,也是自己做个总结.这篇是上,用手机编辑 ...
- Lambda表达式按字段名字排序
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...