Difference between forward and include of RequestDispatcher

forward is used to forward a request, that is control is transfered to the new servler/jsp. u shud take care to not put any our.println() statements in the servlet from where u plan to call forward method. in fact u cant even fire response.getWriter() method in this case. u can only do that in the servlet to which the control is bein fwded

include - means that the new servlet/jsp will be procesed and any out.println()/html stuff will be included and then control will come back to the servlet/jsp that called include method.

 

 

RequestDispatcher接口所定义的forward()方法可以将HTTP请求转送给其他Web资源(例如Servlet、JSP或HTML)进行处理,并产生HTTP回应。

调用forward()方法时必须注意下列两点:

1.      在HTTP回应被“确认”(committed)以前才能调用forward()方法(这里的“确认”是指将HTTP回应的内容主体送回用户端),否则将拋出IllegalStateException异常。

2.      调用forward()方法后,原先存放在HttpResponse对象中的内容将会自动被清除.


RequestDispatcher 接口的include()方法与forward()方法非常类似,惟一的不同在于:利用include()方法将HTTP请求转送给其他Servlet 后,被调用的Servlet虽然可以处理这个HTTP请求,但是最后的主导权仍然是在原来的Servlet。换言之,被调用的Servlet如果产生任何 HTTP回应,将会并入原来的HttpResponse对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值