requestDispatcher笔记

本文详细解释了在Servlet中使用requestDispatcher接口进行请求转发(forward)时的注意事项,特别是关于输出流状态的要求。当Servlet尝试调用forward方法时,响应缓冲区中的数据必须未提交给客户端,否则将抛出异常。此外,还对比了请求包含(include)操作的不同之处。

the forward method of the requestDispatcher interface may be called by the calling servlet only when no output has been committd (通过flush方法)to the client.if output data exists in the response buffer that has not been committed,the content must be cleared before the target sevlet's service method is called .if the response has been commited,an exception must be thrown.

 

//列子

   1. RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/dispatcher");
    2.ServletOutputStream sos=response.getOutputStream();
   3. sos.write("你好".getBytes());
    4.sos.flush();
    5.rd.forward(request, response);

 

当你写了第4行代码时,运行程序会报异常

当你去掉第4行的时候,response buffer中的内容会被清空

 

include 无以上 限制

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值