当前位置:网站首页>Response对象
Response对象
2022-07-23 04:17:00 【陈毓辰】
Response对象
Response的继承体系:
- 接下来我们要了解如下内容:
- Response设置响应数据的功能介绍
- Response完成重定向
- Response响应字符数据(待写)
- Response响应字节数数据(待写)
Response设置响应数据的功能介绍
HTTP响应数据总共分为三部分内容,分别是响应行、响应头、响应体,对于这三部分内容的数据,respone对象都提供了哪些方法来进行设置?
- 响应行 HTTP/1.1 200 ok
对于响应行,比较常用的就是设置响应状态码:
void setStatus(int sc);
- 响应头 Content-Type:text/html 键值对的形式
设置响应头键值对:
void setHeader(String name,String value);
- 响应体
对于响应体,是通过字符、字节输出流的方式往浏览器写,
获取字符输出流:
PrintWriter getWriter();
获取字节输出流
ServletOutputStream getOutputStream();
介绍完这些方法后,后面我们会通过案例把这些方法都用一用,首先来完成一下重定向的功能开发。
Response请求重定向
- Response重定向(redirect):一种资源跳转方式。
(1). 浏览器发送请求给服务器,服务器中对应的资源A接收到请求
(2). 资源A现在无法处理请求,就会给浏览器响应一个302的状态码+location的一个访问资源B的路径
(3). 浏览器接收到响应状态码为302就会重新发送请求到location对应的访问地址去访问资源B
(4). 资源B接收请求后进行处理并最终给浏览器响应结果,这整个过程就叫重定向- 重定向的实现方式:
resp.setStatus(302);
resp.setHeader("location","资源B的访问路径");
具体如何来使用,如下图:
相关代码如下
- 创建ResponseDemo1类,并在ResponseDemo1的doGet方法中给前端响应数据
@WebServlet("/resp1")
public class ResponseDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("resp1....");
//重定向
//1.设置响应状态码 302
response.setStatus(302);
//2. 设置响应头 Location
response.setHeader("Location","/request-demo/resp2");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
- 创建ResponseDemo2类
@WebServlet("/resp2")
public class ResponseDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("resp2....");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
- 启动测试
在控制台看到如下内容:
resp1…
resp2…
这说明两个资源都被访问,重定向已经完成。
虽然功能已经实现,但是从设置重定向的两行代码来看,会发现除了重定向的地址不一样,其他的内容都是一模一样,所以request对象给我们提供了简化的编写方式为:
response.sendRedirect(“重定向的路径”);//这里要注意的是,并不是每一个人的访问路径都一样,要看pom.xml文件中的配置,如果没有配置访问路径的话就是虚拟目录加上资源名
简化开发
@WebServlet("/resp1")
public class ResponseDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("resp1....");
//重定向
resposne.sendRedirect("/request-demo/resp2");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
- 重定向的特点
- 浏览器的地址栏路径发生变化,这相当于两次请求,所以会发生变化
- 可以重定向到任何位置的资源(服务器内,外部均可),而request的转发只是服务器内部的资源所以地址没有发生变化
- 两次请求,不能在多个资源使用request共享数据。因为浏览器发送两个请求,是两个不同的request对象,就无法通过request对象进行共享数据。
介绍完请求重定向和请求转发以后,接下来需要把这两个放在一块对比下:
| 重定向特点: | 请求转发特点: |
|---|---|
| 浏览器地址栏路径发生变化 | 浏览器地址栏路径发生不变化 |
| 可以重定向到任意位置的资源(服务器内部、外部均可) | 只能转发到当前服务器的内部资源 |
| 两次请求,不能在多个资源使用request共享数据 | 一次请求,可以在转发的资源间使用request共享数据 |
以后到底要用哪个,还是需要根据具体业务来决定!!!!
路径问题
- 问题1:转发的时候路径上没有加
/request-demo而重定向加了,那么到底什么时候需要加,什么时候不需要加呢?
其实判断的依据很简单,只需要记住下面的规则即可:
- 浏览器使用:需要加虚拟目录(项目访问路径,但是这个项目访问路径我们可以在pom.xml文件中设置,使得该路径也不需要加虚拟目录名称,如
<path>/</path>) - 服务端使用:不需要加虚拟目录
对于转发来说,因为是在服务端进行的,所以不需要加虚拟目录
对于重定向来说,路径最终是由浏览器来发送请求,就需要添加虚拟目录。
掌握了这个规则,接下来就通过一些练习来强化下知识的学习:
<a href='路径'><form action='路径'>- req.getRequestDispatcher(“路径”)
- resp.sendRedirect(“路径”)
答案:
1.超链接,从浏览器发送,需要加
2.表单,从浏览器发送,需要加
3.转发,是从服务器内部跳转,不需要加
4.重定向,是由浏览器进行跳转,需要加。
- 问题2:在重定向的代码中,
/request-demo是固定编码的,如果后期通过Tomcat插件配置了项目的访问路径,那么所有需要重定向的地方都需要重新修改,该如何优化?
答案也比较简单,我们可以在代码中动态去获取项目访问的虚拟目录,具体如何获取,我们可以借助前面咱们所学习的request对象中的getContextPath()方法,修改后的代码如下:
@WebServlet("/resp1")
public class ResponseDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("resp1....");
//简化方式完成重定向
//动态获取虚拟目录
String contextPath = request.getContextPath();
response.sendRedirect(contextPath+"/resp2");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
重新启动访问测试,功能依然能够实现,此时就可以动态获取项目访问的虚拟路径,从而降低代码的耦合度。
边栏推荐
- 比你老师详细系列————结构体
- [pytorch] the difference between cuda() and to (device)
- Is there a fraud in opening an account with Huatai Securities? Is it safe
- Underlying mechanism of pointer
- The safe distance between you and personal information leakage may be decided by a laptop!
- 金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(二))
- Unityc realizes the conversion of Chinese characters to Pinyin - using Microsoft chspinyinconv Library
- 网线水晶头接法图解8根顺序
- 千亿营收之后,阿里云生态有了新打法
- 32 < tag array and bit operation > supplement: Lt. sword finger offer 56 - I. number of occurrences of numbers in the array
猜你喜欢

缓存穿透、缓存击穿、缓存雪崩

Special training - linked list

Flask learning notes

数据湖:Apache Iceberg介绍

SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?

8.< tag-动态规划和LCS问题>lt.300. 最长递增子序列 + lt.674. 最长连续递增序列

网线水晶头接法图解8根顺序

VirtualBox如何设置端口转发?

Network data leakage events occur frequently, how to protect personal privacy information?

New file / filter / folder in VS
随机推荐
Advantages and disadvantages of RDB and AOF
第四篇章:运行时数据区——共享空间
Kingbasees SQL language reference manual of Jincang database (8. Function (4))
[Internet of vehicles prototype system II] database + application layer protocol design
Sonar中如何删除一个项目
序列模型(三)- 序列模型和注意力机制
金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(九))
Registration tree mode
chrome selenium 用默认profile 不必每次清空
redis伪集群一键部署脚本---亲测可用
[learning notes] graph theory thinking problem
[pytorch] the difference between cuda() and to (device)
网络数据泄露事件频发,个人隐私信息如何保护?
Kingbasees SQL language reference manual of Jincang database (8. Function (III))
Summary of topics related to strings
VirtualBox如何设置端口转发?
Flask学习笔记
使用MindStudio的X2MindSpore工具进行训练脚本转换
SAP 批导模板(WBS批导为例)
No routines, no traps, no advertisements | are you sure you don't need this free instant messaging software?