【JavaWeb】服务器转发和客户端重定向

1.服务器转发

(1)原理

image

(2)演示

demo06

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("demo06……");
		//服务器内部转发
        request.getRequestDispatcher("demo07").forward(request,response);
    }

demo07

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("demo07……");
    }

启动项目访问demo06,只有一次请求,URL地址不变
image

2.客户端重定向

(1)原理

image

(2)演示

demo06
使用response

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("demo06……");
        response.sendRedirect("demo07");
    }

demo07

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("demo07……");
    }

启动项目访问demo06,客户端两次请求,URL地址变为demo07
image

posted @ 2022-07-24 21:40  植树chen  阅读(80)  评论(0编辑  收藏  举报