1.概念【响应给浏览器】
  • 响应∶回馈结果。在B/S架构中,就是服务器给客户端浏览器反馈结果。
  • 响应对象∶就是在项目中用于发送响应的对象。
  • 实现接口:ServletResponse和HttpServletResponse【浏览器访问服务器后,服务器给客户端响应的数据会封装为ServletResponse对象,它有一个子类叫HttpServletResponse对象,用于封装按照Http协议封装的响应数据。】
2.响应状态码

3.字节/字符流响应数据

字节流响应数据【响应给浏览器】

@WebServlet("/ServletDemo1")
public class ServletDemo1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//设置浏览器响应编码[html类型的文本,字符集为utf-8]resp.setContentType("text/html;charset=utf-8");//1.获取字节流输出对象ServletOutputStream os = resp.getOutputStream();//2.定义一个消息String str ="你好世界";//3.通过字节流对象输出os.write(str.getBytes("utf-8"));}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}

字符流响应数据【响应给浏览器】

@WebServlet("/ServletDemo2")
public class ServletDemo2 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//设置浏览器响应编码[html类型的文本,字符集为utf-8]resp.setContentType("text/html;charset=utf-8");//获取字符流输出对象resp.getWriter().write("<h3>这是一个响应信息</h3>");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}

注:【字节流响应数据无法换行,原因:字节流读取1024个字节,直接读到结尾,然后在末尾写入,相当于换行符没起到最用,可以采用字符流展示标签数据】

4.响应图片[图片不需要设置编码]

点击超链接对应ServletDemo3处理

<a href="/Response/ServletDemo3">点我看图</a>
//响应图片[图片不需要设置编码]
@WebServlet("/ServletDemo3")
public class ServletDemo3 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.创建字节输入流对象,关联读取图片路径//首先获取到关联图片的项目路径[因为项目发布后路径会改变]String realPath = getServletContext().getRealPath("/img/nan.png");BufferedInputStream bis = new BufferedInputStream(new FileInputStream(realPath));//2.响应对象获取字节输出流对象,展示关联图片ServletOutputStream os = resp.getOutputStream();byte[] bytes = new byte[1024];int len = bis.read(bytes);while (len!=-1){os.write(bytes,0,len);len = bis.read(bytes);}bis.close();}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
5.设置缓存时间
  • 缓存:对于不经常变化的数据,我们可以设置合理缓存时间,以避免浏览器频繁请求服务器。以此来提高效率
  • 方法:setDateHeader(String name,long time)
//设置缓存
@WebServlet("/ServletDemo4")
public class ServletDemo4 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//设置浏览器响应编码[html类型的文本,字符集为utf-8]resp.setContentType("text/html;charset=utf-8");String news = "这是一条很火爆的新闻";//设置缓存[1小时缓存时间]resp.setDateHeader("Expires", System.currentTimeMillis() + 1 * 60 * 60 * 1000);//写出数据resp.getWriter().write(news);System.out.println("只有第一次输出,再次访问不会访问服务器");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}打印结果://浏览器不刷新就会读取缓存
---------------------------------------------------------
只有第一次输出,再次访问不会访问服务器
6.设置定时刷新
//定时刷新
@WebServlet("/ServletDemo5")
public class ServletDemo5 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//设置浏览器响应编码[html类型的文本,字符集为utf-8]resp.setContentType("text/html;charset=utf-8");//写出显示数据resp.getWriter().write("您的用户名有误,3秒回自动跳转到登录页面");//定时刷新,跳回resp.setHeader("Refresh","3;url=/Response/html/index.html");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
7.重定向
//重定向a找b,b告诉a,c可以完成,a再找c   a找了2次
@WebServlet("/ServletDemo6")
public class ServletDemo6 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//设置浏览器响应编码[html类型的文本,字符集为utf-8]resp.setContentType("text/html;charset=utf-8");//方法1:设置重定向状态码/*resp.setStatus(302);resp.setHeader("location",req.getContextPath()+"/ServletDemo5");
*///方法2:resp.sendRedirect(req.getContextPath()+"/ServletDemo5");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
8.文件下载

点击超链接对应ServletDemo7处理

<a href="/Response/ServletDemo7">点我下载图片</a>
//文件下载
@WebServlet("/ServletDemo7")
public class ServletDemo7 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.创建字节输入流对象,关联读取的文件String realPath = getServletContext().getRealPath("/img/nan.png");BufferedInputStream bis = new BufferedInputStream(new FileInputStream(realPath));//2.设置响应头支持的类型/*Content-Type 消息头名称支持的类型application/octet-stream消息头参数应用的类型为字节流*/resp.setHeader("Content-Type","application/octet-stream");//3.设置响应头以下载方式打开附件/*Content-Disposition消息头名称 处理的形式attachment ;filename=hm.png消息头参数附件形式进行处理―指定下载文件的名称*/resp.setHeader("Content-Disposition","attachment;filename=nan.png");//4.获取字节输出流对象ServletOutputStream os = resp.getOutputStream();//5.循环读写byte[] bytes = new byte[1024];int len = bis.read(bytes);while (len!=-1){os.write(bytes,0,len);len = bis.read(bytes);}//6.释放资源bis.close();}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}

响应对象Response相关推荐

  1. python中response对象的方法_响应对象response

    响应对象response flask提供了Response最为视图最终返回的数据,但在代码编写过程中,我们通常很少直接使用Response对象,下面是几个常见的视图返回数据的方式 from flask ...

  2. python中response对象的属性_Django 中的响应对象 Response

    视图在接收请求并处理后,必须返回HttpResponse对象或子对象.HttpRequest对象由Django创建,HttpResponse对象由开发人员创建. 一.HttpResponse: 可以使 ...

  3. 设置响应对象的编码格式

    设置响应对象的编码格式 爬虫爬取到的数据乱码? 方式一:在发送请求完获取响应对象response对象后,需要对response对象设置编码格式 response.encoding = response ...

  4. java 请求响应_java http接口请求响应 request response

    接口类: 1 package org.sunshine.dcda.epg.wechat.controller.niao; 2 3 import javax.servlet.http.HttpServl ...

  5. Django请求响应对象

    请求与响应对象 HttpRequest HttpRequest存储了客户请求的相关参数和一些查询方法. path 请求页面的全路径,不包括域名-例如, "/hello/". met ...

  6. jsp内置对象--response

    response:响应对象 提供的方法:void addcookie(Cookie cookie);服务端向客户端增加一个cookie对象 void sendRedirect(String locat ...

  7. JSP的9个内置对象-response

    response代表服务器对客户端的响应.大部分时候,程序无须使用response来响应客户端请求,因为有个更简单的响应对象-out.它是页面输出流,是JstWriter的实例.JspWriter是W ...

  8. 创建Session时会把含有Session ID 的Cookie对象加到响应对象上

    从逻辑角度讲,我们应该会这样做,因为创建Session的目的,在于保存会话信息,所以要把SessionID传给浏览器,以便后面的请求能找到服务器中自己的Session,来获取自己的会话信息. 我们来分 ...

  9. Flask的Rsponse响应对象

    返回值可以为元组.字符串.字典.response对象和make_response对象 from flask import Flask, Response, make_responseapp=Flask ...

最新文章

  1. ASP.NET中gridview获取当前行的索引值
  2. Miniconda3+PyTorch1.7.1(GPU版)+Win10_x64+GTX1060深度学习环境搭建
  3. ext中给文本框赋值的方法_大多数人不知道的Python合并字典的七种方法
  4. 人工智能:第九章 Agent (艾真体)
  5. 【LeetCode 55】【LeetCode 45】 跳跃游戏
  6. 单例模式创建的两种方法
  7. 入门指南_激光切管快速入门指南
  8. 跟小伙伴们做了个高效刷论文的小站
  9. for循环执行 mybatis_Mybatis中使用循环遍历
  10. python结构_Python 项目的结构
  11. linux http 访问限制,51CTO博客-专业IT技术博客创作平台-技术成就梦想
  12. BZOJ1305 [CQOI2009]dance跳舞 【网络流】
  13. TYVJ1467 通往聚会的道路
  14. 卸载win10预装软件和小娜
  15. 常微分方程 伍卓群 题目
  16. hhkb mac设置_HHKB 键盘 使用攻略 Karabiner
  17. android p 小米6,小米6还能再战几年!将升级Android P
  18. xaxis python_在python中绘制xaxis中的多列值
  19. canvas画圆环(一)之渐变色,纯色
  20. 数字化助力生产制造管理:专项生产管理系统

热门文章

  1. B. Box Fitting
  2. hdu-1114 Piggy-Bank
  3. 容斥问卷调查反馈——Co-prime,Character Encoding,Tree and Constraints,「2017 山东一轮集训 Day7」逆序对
  4. [TJOI2018]智力竞赛 (匈牙利)
  5. [2020.10.25NOIP模拟赛]序列【Splay】
  6. ssl初一组周六模拟赛【2018.5.19】
  7. Codeforces Round #663 (Div. 2)
  8. 2017西安交大ACM小学期数论 [更新学号]
  9. 动态规划训练9 [Brackets POJ - 2955 ]
  10. 如何使用MAT进行JVM内存泄露分析