Request和Response学习笔记5

服务器内部请求的转发

这里请求传递所使用的方法:

// 通过request对象获取请求转发器对象
RequestDispatcher getRequestDispatcher(String path)

// 使用RequestDispatcher对象来进行转发
forward(ServletRequest request, ServletResponse response) 

实例引入

  1. 创建一个Web项目:RequestDelivery

  2. 创建两个类,在这两个类之间进行请求的传递:

    1. RequestDemo01.java

      import javax.servlet.RequestDispatcher;
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;
      
      @WebServlet("/demo01")
      public class RequestDemo01 extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
              System.out.println("demo01被访问了!");
              // 获取请求发送对象
              RequestDispatcher requestDispatcher = request.getRequestDispatcher("/demo02");
              // 发送请求
              requestDispatcher.forward(request, response);
          }
          @Override
          protected void doPost(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
              this.doGet(request, response);
          }
      }
      
      
    2. RequestDemo02.java

      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;
      
      @WebServlet("/demo02")
      public class RequestDemo02 extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
              System.out.println("我是demo02,demo01的请求对象已经传递过来了!");
          }
          @Override
          protected void doPost(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
              this.doGet(request, response);
          }
      }
      
  3. 启动服务器,访问:http://localhost:8080/RequestDelivery_war_exploded/demo01

    控制台输出:

    demo01被访问了!
    我是demo02,demo01的请求对象已经传递过来了!
    

    页面不会跳转,还在在demo1页面。

特点

  1. 浏览器地址栏路径不发生变化
  2. 只能转发到当前服务器内部资源中。不能转发到其他服务器。
  3. 用户的发送请求是一次,被转发的请求也是用户发送的请求对象。

Request共享数据

  1. 备注:

    • 域对象:一个有作用范围的对象,可以在一定范围内共享数据。
    • request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据。
  2. 方法:

    // 存储数据
    void setAttribute(String name,Object obj)
    
    // 通过键获取值
    Object getAttitude(String name)
    
    // 通过键移除键值对
    void removeAttribute(String name)
    

实例引入

创建两类:

  1. RequestDemo03.java

    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet("/demo03")
    public class RequestDemo03 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            System.out.println("demo03被访问了!");
            // 获取请求发送对象
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/demo04");
            // 发送请求
            requestDispatcher.forward(request, response);
        }
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            this.doGet(request, response);
        }
    }
    
  2. RequestDemo04.java

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet("/demo04")
    public class RequestDemo04 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            System.out.println("我是demo04,demo03的请求对象已经传递过来了!");
        }
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            this.doGet(request, response);
        }
    }
    

存储数据

  1. 修改RequestDemo03.java中的doGet()方法:

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("demo03被访问了!");
        // 存储数据到request域中
        request.setAttribute("Set's Message", "Hello, setAttribute!");
    
        // 获取请求发送对象
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/demo04");
        // 发送请求
        requestDispatcher.forward(request, response);
    }
    
  2. 修改RequestDemo04.java中的doGet()方法:

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("我是demo04,demo03的请求对象已经传递过来了!");
        // 获取request域数据
        System.out.println(request.getAttribute("Set's Message"));
    }
    
  3. 启动服务器,访问:http://localhost:8080/RequestDelivery_war_exploded/demo03

    控制台输出:

    demo03被访问了!
    我是demo04,demo03的请求对象已经传递过来了!
    Hello, setAttribute!
    

参考文献

posted @ 2021-03-03 22:45  LeeHua  阅读(127)  评论(0编辑  收藏  举报