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);}
}