请求转发:一种在服务器内部的资源跳转的方式

  实现:

 1 import javax.servlet.RequestDispatcher;
 2 import javax.servlet.ServletException;
 3 import javax.servlet.annotation.WebServlet;
 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 /**
10  * @Author: 艾康
11  * @Date: 2019/10/15 14:35
12  */
13 @WebServlet("/servlettest3")
14 public class ServletTest3 extends HttpServlet {
15     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
16     //点击登录后跳转到另一个html
17         //步骤:
18         //1.通过request对象获取请求转发器对象
19         RequestDispatcher requestDispatcher = request.getRequestDispatcher("/aaa.html");
20         //2.使用RequestDispatcher对象进行转发
21         requestDispatcher.forward(request,response);
22     }
23
24     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
25         this.doPost(request,response);
26     }
27 }

  2.特点:

    1.浏览器地址栏路径不发生变化

    2.只能转发到当前服务器内部资源中

    3.只转发一次请求

01-15 04:52