JavaWeb——Response设置响应数据功能和amp;完成重定向和amp;资源路径问题
在体系结构上与Request对象相似。
Reponse设置响应数据功能介绍
响应数据结构
响应行有三部分组成,协议 状态码 状态描述
响应头和请求头一样都是键值对的形式
响应体和请求体一样也是通过流的方式在浏览器之间传输数据,字符就是纯文本,字节数据比如说图片数据
Response完成重定向
重定向:一种资源跳转方式
第一步:
设置响应状态码302,让浏览器去请求别的资源
第二步:
设置响应头location
第三步:
浏览器访问重定向的资源
准备两个资源A和B和一个前端页面rep.html
资源A
-
-
public class ResponseDemo1 extends HttpServlet {
-
-
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");
-
}
-
-
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
this.doGet(request, response);
-
}
-
}
资源B
-
-
-
public class ResponseDemo2 extends HttpServlet {
-
-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
System.out.println("resp2....");
-
}
-
-
-
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
this.doGet(request, response);
-
}
-
}
前端页面的表单
-
<form action="/request-demo/resp1" method="get">
-
<input type="text" name="username"><br>
-
<input type="password" name="password"><br>
-
<input type="checkbox" name="hobby" value="1"> 游泳
-
<input type="checkbox" name="hobby" value="2"> 爬山 <br>
-
<input type="submit">
-
</form>
注意:这里要使用无痕浏览页面访问
提交表单后的输出如下
两个资源都被访问了
简化方式完成重定向
资源A修改
-
-
public class ResponseDemo1 extends HttpServlet {
-
-
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");*/
-
-
//简化方式完成重定向
-
response.sendRedirect(contextPath "/resp2");
-
//response.sendRedirect("https://www.bilibili.com");
-
}
-
-
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
this.doGet(request, response);
-
}
-
}
重定向的资源也可以是其他服务器的资源
重定向特点:
有两个request域,不能共享数据
路径问题
重定向和转发需要的路径不同,重定向多一个虚拟目录
-
-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
System.out.println("resp1....");
-
//简化方式完成重定向
-
//动态获取虚拟目录
-
String contextPath = request.getContextPath();
-
// response.sendRedirect(contextPath "/resp2");
-
response.sendRedirect("https://www.bilibili.com");
-
}
虚拟目录的路径地址可以通过pom.xml下的path标签动态修改虚拟目录地址
动态获取虚拟目录
降低耦合性
-
-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
System.out.println("resp1....");
-
//简化方式完成重定向
-
//动态获取虚拟目录
-
String contextPath = request.getContextPath();
-
response.sendRedirect(contextPath "/resp2");
-
// response.sendRedirect("https://www.bilibili.com");
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgakcag
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01