Cannot forward a response that is already committed
来源:互联网 发布:2016中国台湾贸易数据 编辑:程序博客网 时间:2024/05/22 11:29
- 异常一:java.lang.IllegalStateException:Cannot forward a response that is already committed
异常二:IllegalStateException:response already commited
异常三:IllegalStateException:getOutputStream() has already been called for this request
前言:
response是服务端对客户端请求的一个响应,其中封装了响应头、状态码、内容(也就是最终要在浏览器上显示的HTML代码或者其他数据格式)等,
服务端在把response提交到客户端之前,会使用一个缓冲区,并向该缓冲区内写入响应头和状态码,然后将所有内容flush(flush包含两个步骤:先将缓冲区内容发送至客户端,然后将缓冲区清空)。这就标志着该次响应已经committed(提交)。对于当前页面中已经committed(提交)的response,就不能再使用这个response向缓冲区写任何东西 (注:以为JSP中,response是一个JSP页面的内置对象,所以同一个页面中的response.XXX()是同一个response的不同方法,只要其中一个已经导致了committed,那么其它类似方式的调用都会导致 IllegalStateException异常)。
对于异常一:
问题描述:
在执行一段代码后,最后执行request.getRequestDispatcher(跳转页面URL).forward(resquest,response)进行页面跳转时,报了此错
问题原因:
1.在forwar之前,response就已经commit到浏览器端了。
导致response commit的原因包括:forward, redirect, flushBuffer
2.前两个原因都是指在forward之前就已经进行了页面跳转动作(forward, redirect)。
至于flushBuffer,每一个JSP页面都有一个缓冲区,默认的缓冲区大小为8KB,如果缓冲区被占满的话,web服务器就会自动将response 进行commit,然后清空缓冲区(即flush),所以再进行页面跳转的时候就会报response已经被commit了。
内置对象out相关方法
public abstract void clear() throws java.io.IOException 清除缓冲区中的内容。如果缓冲区已经被刷新,clear()方法将抛出IOException异常 public abstract void clearBuffer() throws java.io.IOException 清除缓冲区中的当前内容。这个方法和clear()方法的区别是,如果缓冲区已经被刷新,这个方法不会抛出IOException异常 public abstract void close() throws java.io.IOException刷新缓冲区,关闭输出流。注意,我们在编写JSP页面时,不需要显式地去调用这个方法,因为在JSP容器所生成的代码中会自动包含对close()方法的调用。
public abstract void flush() throws java.io.IOException 刷新缓冲区,两个步骤:1,提交response 2.清空缓冲区 public int getBufferSize() 获得缓冲区大小,同response.getBufferSize()相同 public abstract int getRemaining() 获得缓冲区中未使用的字节数 public boolean isAutoFlush() 判断out对象是否是自动刷新<%@ page autoFlush="true" %> <%--Default--%>
内置对象response相关方法
response.isCommitted() 确认response是否已经committed response.flushbuffer(); 同out.flush相同
解决方法:
增大缓冲区大小:
<%@ page buffer="10kb" %>
参考:http://jorton468.blog.163.com/blog/static/72588135201102441617287/
- Cannot forward a response that is already committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- Cannot forward after response has been committed
- ORA-06512 get_ddl
- Qt显示Flash的两种方法 QWebview和QAxWidget
- ASP.NET中Server.MapPath() 和 Request.MapPath()使用区别(于三份资料中提取)
- 编码转换的记录
- 身份证号码15位升18位规则
- Cannot forward a response that is already committed
- EXISTS 与 IN 的 比较
- pca 特征抽取
- mesa EGL
- oracle三种复制技术之比较
- C#操作App.config
- 关于SHFileOperation
- Simulink中的tunable参数
- 未能加载文件或程序集“Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”或它的某一个依赖项。系统找不到指定的