HttpServletResponse
1、command键+鼠标左键查看HttpServletResponse的源码,然后再查看它继承的类ServletResponse的源码
负责向浏览器发送数据的方法:
ServletOutputStream getOutputStream() throws IOException; PrintWriter getWriter() throws IOException;
负责向浏览器发送响应头的方法:
1 void setCharacterEncoding(String var1); 2 3 void setContentLength(int var1); 4 5 void setContentLengthLong(long var1); 6 7 void setContentType(String var1); 8 void setDateHeader(String var1, long var2); 9 10 void addDateHeader(String var1, long var2); 11 12 void setHeader(String var1, String var2); 13 14 void addHeader(String var1, String var2); 15 16 void setIntHeader(String var1, int var2); 17 18 void addIntHeader(String var1, int var2);
可以在HttpServletResponse中查看到一些常量,响应的状态码,常见的有200、300、405、500、502等
2、常见应用
(1)向浏览器输出消息
(2)下载文件
获取下载的路径
下载的文件名是啥
设置想办法让浏览器能够支持下载我们需要的东西
获取下载文件的输入流
创建缓冲区
获取OutputStream对象
将FileOutoutstream流写入到buffer缓冲区
使用OutputStream将缓冲区中的数据输出到客户端
新建一个response工程,按照之前的套路部署好文件、tomcat等
然后放置一张图片在resourese目录下,
Fileservlet中的代码:
package com.liyu.servlet; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.FileInputStream; import java.io.IOException; public class FileServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取下载的路径 String realPath = "/Users/dailingyi/IdeaProjects/javaweb-02-servlet1/response/target/classes/1.png"; System.out.println("下载的文件路径:"+realPath); // 下载的文件名是啥 String filename = realPath.substring(realPath.lastIndexOf("\\") + 1); // 设置想办法让浏览器能够支持Content-Disposition下载我们需要的东西,如果是中文文件名还需要用URLEncoder.encode编码,否则有可能乱码 resp.setHeader("Content-Disposition","attachment;filemame="+filename); // 获取下载文件的输入流 FileInputStream in = new FileInputStream(realPath); // 创建缓冲区 int len = 0; byte[]buffer = new byte[1024]; // 获取OutputStream对象 ServletOutputStream out = resp.getOutputStream(); // 将FileOutoutstream流写入到buffer缓冲区, 使用OutputStream将缓冲区中的数据输出到客户端 while ((len = in.read(buffer))>0){ out.write(buffer,0,len); } in.close(); out.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
然后在web.xml中配置,运行即可在网页下载1.png图片
(3)验证码功能
前端实现
后端实现,需要用到java的图片类,生成一个图片
ImageServlet java类
1 package com.liyu.servlet; 2 3 import javax.imageio.ImageIO; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import java.awt.*; 9 import java.awt.image.BufferedImage; 10 import java.io.BufferedInputStream; 11 import java.io.IOException; 12 import java.util.Random; 13 14 public class ImageServlet extends HttpServlet { 15 @Override 16 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 17 //如何让浏览器5秒自动刷新一次 18 resp.setHeader("refresh","3"); 19 20 //在内存中创建一个图片 21 BufferedImage image = new BufferedImage(80,20,BufferedImage.TYPE_INT_RGB); 22 23 //得到图片 24 Graphics2D g =(Graphics2D) image.getGraphics(); //笔 25 //设置图片的背景颜色 26 g.setColor(Color.white); 27 g.fillRect(0,0,80,20); 28 //给图片写数据 29 g.setColor(Color.BLUE); 30 g.setFont(new Font(null,Font.BOLD,20)); 31 g.drawString(makeNum(),0,20); 32 33 //告诉浏览器,这个请求用图片的方式打开 34 resp.setContentType("Image/jpg"); 35 //网站存在缓存,不让浏览器缓存 36 resp.setDateHeader("expires",-1); 37 resp.setHeader("Cache-Control","no-cache"); 38 resp.setHeader("Pragma","no-cache"); 39 40 //把图片写给浏览器 41 boolean write = ImageIO.write(image,"jpg",resp.getOutputStream()); 42 43 } 44 45 //生成随机数 46 private String makeNum(){ 47 Random random = new Random(); 48 String num = random.nextInt(9999999) + ""; 49 StringBuffer sb = new StringBuffer(); 50 for (int i = 0; i<7-num.length();i++){ 51 sb.append("0"); 52 } 53 num = sb.toString()+num; 54 return num; 55 } 56 @Override 57 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 58 59 } 60 }
然后在web.xml注册,运行测试
出现验证码,且3秒更新一次
(4)实现重定向
一个web资源收到客户端请求后,它会通知客户端去访问另外一个web资源,这个过程叫重定向
常见场景:用户登录
1 void sendRedirect(String var1) throws IOException;
新建一个RedirectServlet,
1 package com.liyu.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.IOException; 8 9 public class RedirectServlet extends HttpServlet { 10 @Override 11 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 12 resp.sendRedirect("/response_war/img"); //重定向 13 } 14 15 @Override 16 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 17 doGet(req, resp); 18 } 19 }
然后在web.xml注册
运行测试,访问http://localhost:8080/response_war/red会自动跳转到http://localhost:8080/response_war/img