1. Request

1.1 请求对象介绍

  • 请求:获取资源。在 BS 架构中,就是客户端浏览器向服务器端发出询问。JavaEE工程中,客户浏览器发出询问,遵循HTTP协议所规定.
  • 请求对象:就是在项目当中用于发送请求的对象。

image.png

1.2 常用请求对象

image.png

1.3 请求对象常用方法-获取各路径

image.png

1.4 请求对象常用方法-获取请求头信息

image.png

1.5 请求对象常用方法-获取请求参数信息

image.png

1.6 获取请求参数并封装对象

  • 手动封装方式。
  • 反射封装方式。
  • 工具类封装方式,使用BeanUtils

    1. @WebServlet(value = "/servletDemo03")
    2. public class RequestDemo03 extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    5. Map<String, String[]> map = req.getParameterMap();
    6. Student stu = new Student();
    7. try {
    8. BeanUtils.populate(stu,map);
    9. } catch (IllegalAccessException e) {
    10. e.printStackTrace();
    11. } catch (InvocationTargetException e) {
    12. e.printStackTrace();
    13. }
    14. System.out.println(stu);
    15. }
    16. }

    1.7 流对象获取请求信息

    image.png

    1. @WebServlet(value = "/servletDemo04")
    2. public class RequestDemo04 extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    5. req.setCharacterEncoding("UTF-8");
    6. //字符流
    7. BufferedReader reader = req.getReader();
    8. String line = null;
    9. while ((line = reader.readLine()) != null) {
    10. System.out.println(line);
    11. }
    12. }
    13. @Override
    14. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    15. this.doGet(req,resp);
    16. }
    17. }

    1.8 中文乱码问题

    GET 方式
    没有乱码问题。在 Tomcat 8.5 版本后已经解决!
    POST 方式
    有乱码问题。可以通过 setCharacterEncoding() 方法来解决!

    1.9 请求域

  • 请求域(request 域):可以在一次请求范围内进行共享数据。

  • 一般用于请求转发的多个资源中共享数据。
  • 请求对象操作共享数据方法:

image.png
image.png

1.10 请求转发

  • 请求转发:客户端的一次请求到达后,发现需要借助其他 Servlet 来实现功能。
  • 特点:
    • 浏览器地址栏不变
    • 域对象中的数据不丢失
    • 负责转发的 Servlet 转发前后的响应正文会丢失
    • 由转发的目的地来响应客户端

image.png

image.png

  1. @WebServlet(value = "/servletDemo09")
  2. public class RequestDemo09 extends HttpServlet {
  3. @Override
  4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  5. //1.拿到请求调度对象
  6. RequestDispatcher rd = req.getRequestDispatcher("/servletDemo10");
  7. req.setAttribute("encoding", "utf-8");
  8. rd.forward(req, resp);
  9. }
  10. @Override
  11. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  12. this.doGet(req, resp);
  13. }
  14. }
  15. @WebServlet(value = "/servletDemo10")
  16. public class RequestDemo10 extends HttpServlet {
  17. @Override
  18. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  19. String encoding = (String) req.getAttribute("encoding");
  20. System.out.println(encoding);
  21. System.out.println("Servlet10请求到了。。。。");
  22. }
  23. @Override
  24. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  25. this.doGet(req, resp);
  26. }
  27. }

2. Response

2.1 响应对象介绍

  • 响应:回馈结果。在 BS 架构中,就是服务器给客户端浏览器反馈结果。
  • 响应对象:就是在项目中用于发送响应的对象。

image.png

2.2 常见响应状态码

image.png

2.3 字节流响应详细

image.png

  1. @WebServlet("/servletDemo01")
  2. public class ServletDemo01 extends HttpServlet {
  3. @Override
  4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  5. String str = "阿里嘎多";
  6. resp.setContentType("text/html;charset=UTF-8");
  7. ServletOutputStream ops = resp.getOutputStream();
  8. ops.write(str.getBytes("UTF-8"));
  9. ops.close();
  10. }
  11. @Override
  12. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  13. this.doGet(req, resp);
  14. }
  15. }

2.4 字符流响应消息

image.png

  1. @WebServlet("/servletDemo02")
  2. public class ServletDemo02 extends HttpServlet {
  3. @Override
  4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  5. String str = "我是字符流响应的消息";
  6. resp.setContentType("text/html;charset=UTF-8");
  7. //获取字符流对象
  8. PrintWriter writer = resp.getWriter();
  9. writer.write(str);
  10. writer.close();
  11. }
  12. @Override
  13. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  14. this.doGet(req, resp);
  15. }
  16. }

2.5 案例:响应一张图片给浏览器

  1. @WebServlet("/servletDemo03")
  2. public class ServletDemo03 extends HttpServlet {
  3. @Override
  4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  5. //通过文件的相对路径来获取文件的绝对路径
  6. String path = getServletContext().getRealPath("/img/hm.png");
  7. //用字节流来传输图片等文件
  8. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
  9. ServletOutputStream os = resp.getOutputStream();
  10. //循环读写
  11. byte[] arr = new byte[1024];
  12. int len;
  13. while((len = bis.read(arr)) != -1){
  14. os.write(arr,0,len);
  15. }
  16. //关闭资源
  17. bis.close();
  18. }
  19. @Override
  20. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  21. this.doGet(req, resp);
  22. }
  23. }

案例注意点:

  • 上传图片文件的目录不能按照/web/img/hm.png 这样的相对路径写,因为实际路径是在通过tomcat处理的,所以建议使用绝对经,用getServletContext().getRealPath(相对路径)来获取。
  • javaEE项目中的相对路径是相对于Web目录下的,而不是src。
  • 关闭资源只需要关闭自己创建的流即可,因为servlet创建的流不需要自己手动关闭,servlet会帮助关闭的。

    2.6 设置缓存

    缓存:对于不经常变化的数据,我们可以设置合理缓存时间,以避免浏览器频繁请求服务器。以此来提高效率!
    image.png

    1. @WebServlet("/servletDemo04")
    2. public class ServletDemo04 extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    5. String news = "这是一条很火的新闻....";
    6. //设置缓存时间为3秒
    7. resp.setDateHeader("Exprise",System.currentTimeMillis()+1000*3);
    8. resp.setContentType("text/html;charset=UTF-8");
    9. PrintWriter wt = resp.getWriter();
    10. wt.write(news);
    11. System.out.println("aaaa");
    12. }
    13. @Override
    14. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    15. this.doGet(req, resp);
    16. }
    17. }

    注意:设置的请求头为Exprise,表示缓存结束时间,下图的 Date为缓存开始时间。
    image.png

    2.7 设置定时刷新

  • 定时刷新:过了指定时间后,页面自动进行跳转

image.png

  1. @WebServlet("/servletDemo05")
  2. public class ServletDemo05 extends HttpServlet {
  3. @Override
  4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  5. String news = "您的用户名或密码错误,3秒后自动跳转到百度页面...";
  6. //设置编码格式
  7. resp.setContentType("text/html;charset=UTF-8");
  8. //写出数据
  9. resp.getWriter().write(news);
  10. //定时3秒后刷新页面跳转到百度官网
  11. resp.setHeader("Refresh","3;URL=https://www.baidu.com/");
  12. }
  13. @Override
  14. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  15. this.doGet(req, resp);
  16. }
  17. }

2.8 请求重定向

  • 请求重定向:客户端的一次请求到达后,发现需要借助其他 Servlet 来实现功能。
  • 特点:2次请求,浏览器地址栏发生改变

image.png
请求servletDemo06,触发重定向到servletDemo07

  1. @WebServlet("/servletDemo06")
  2. public class ServletDemo06 extends HttpServlet {
  3. @Override
  4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  5. //设置请求与数据
  6. req.setAttribute("name","张三");
  7. //设置重定向
  8. resp.sendRedirect(req.getContextPath()+"/servletDemo07");
  9. }
  10. @Override
  11. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  12. this.doGet(req, resp);
  13. }
  14. }
  15. @WebServlet("/servletDemo07")
  16. public class ServletDemo07 extends HttpServlet {
  17. @Override
  18. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  19. System.out.println("servletDemo07执行了...");
  20. Object name = req.getAttribute("name");
  21. System.out.println(name);
  22. }
  23. @Override
  24. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  25. this.doGet(req, resp);
  26. }
  27. }

控制台输出结果:
image.png
image.png

2.9 案例:文件下载

  1. @WebServlet("/servletDemo08")
  2. public class ServletDemo08 extends HttpServlet {
  3. @Override
  4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  5. //1.创建字节输入流,关联读取的文件
  6. //获取文件的绝对路径
  7. String realPath = getServletContext().getRealPath("/img/hm" + System.currentTimeMillis() + ".png");
  8. //创建字节输出流对象
  9. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(realPath));
  10. //2.设置响应头支持的类型 应用支持的类型为字节流
  11. /*
  12. Content-Type 消息头名称 支持的类型
  13. application/octet-stream 消息头参数 应用类型为字节流
  14. */
  15. resp.setHeader("Content-Type", "application/octet-stream");
  16. //3.设置响应头以下载方式打开 以附件形式处理内容
  17. /*
  18. Content-Disposition 消息头名称 处理的形式
  19. attachment;filename= 消息头参数 附件形式进行处理
  20. */
  21. resp.setHeader("Content-Disposition", "attachment;filename=hm.png");
  22. //4.获取字节输出流对象
  23. ServletOutputStream ops = resp.getOutputStream();
  24. //5.循环读写文件
  25. byte[] arr = new byte[1024];
  26. int len;
  27. while ((len = bis.read(arr)) != -1) {
  28. ops.write(arr, 0, len);
  29. }
  30. //6.释放资源
  31. bis.close();
  32. }
  33. @Override
  34. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  35. this.doGet(req, resp);
  36. }
  37. }

和上面的响应图片案例差不多,在那基础上添加了设置响应头
resp.setHeader("Content-Type", "application/octet-stream");
resp.setHeader("Content-Disposition", "attachment;filename=hm.png");
设置响应头,类型为字节流,浏览器响应的形式是以附件形式进行处理,即浏览器将文件下载到本地。