转发和重定向的对比

转发和重定向的区别:

1、  转发不会改变浏览器的地址栏,而重定向会

2、  转发是在服务器端执行跳转,重定向是在浏览器端执行跳转

3、  转发客户端只发一次请求,重定向整个过程会发两次请求

4、  转发可以传递数据,而重定向不可以

5、  转发只是进行域内跳转,重定向可以跳转到其他应用(如:www.baidu.com)


转发:

servletDemo1:

@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ServletDemo1() {
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*转发*/
		System.out.println("A:我要办事");
		System.out.println("B:你的事我办不了,我可以找人给你办");
		ServletContext application = this.getServletContext();
		application.getRequestDispatcher("/demo2").forward(request, response);
		System.out.println("事办完了");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

servletDemo2:

@WebServlet("/demo2")
public class ServletDemo2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ServletDemo2() {
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("C:***我能帮你办***");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

当在浏览器中访问demo1时,demo1将请求(由服务器端的servletContext域对象)转发给demo2,并且在转发前后浏览器地址栏不会发生变化。

由输出结果看出,在执行跳转后,程序还是回到demo1执行剩余的代码。

转发数据的共享:

       更该上面程序的代码:

@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ServletDemo1() {
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*转发*/
		ServletContext application = this.getServletContext();//两个servlet共享域数据
		application.setAttribute("name", "tom");
		application.getRequestDispatcher("/demo2").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

@WebServlet("/demo2")
public class ServletDemo2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ServletDemo2() {
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext application = this.getServletContext();//两个servlet共享域数据
		String name = (String)application.getAttribute("name");
		System.out.println(name);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}


因为这是一次访问过程,servletContext是域对象,所以进行跳转后,在demo2中可以得到demo1中的数据,因此转发可以共享数据。

 

重定向:

这是servletDemo7

@WebServlet("/demo7")
public class ServletDemo7 extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public ServletDemo7() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//请求重定向
		System.out.println("A:我要借钱");
		System.out.println("B:我没钱,但我可以告诉你谁有");
		response.sendRedirect("/day09_response/demo8");		 System.out.println("事办完了");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

这是servletDemo8

WebServlet("/demo8")
public class ServletDemo8 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ServletDemo8() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("C:我借给你...");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

当在浏览器端访问servletDemo7时,浏览器的地址


访问结束后,浏览器的地址

由此可以看出,进行重定向改变了浏览器的地址栏信息。

 

执行结果:


由执行结果看出,在servletDemo7进行重定向之前,会把其中的所有代码执行完之后,在进行重定向操作。重定向时将新的url地址发给浏览器,再由浏览器进行重新访问,因此会改变浏览器地址栏的信息。所以重定向会发送两次访问请求。

若上面代码为:response.sendRedirect(“http://www.baidu.com”);会跳转到百度


用一张图来表示上述的过程:



好了,就写这么多,排版不太好看,各位大佬凑合看看!



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值