对于初学者来说,一个常见的误解是:当调用 forward() 或者 sendRedirect() 时控制流将会自动跳出原函数。标题所示错误通常是基于此误解而引起的。
示例代码:
protected void doPost() { if (someCondition) { sendRedirect(); } forward(); // This is STILL invoked when someCondition is true!}forward() 和 sendRedirect() 与system.exit() 不同,当上例中的 someCondition为true时,很有可能得到此异常:
IllegalStateException: Cannot forward a response that is already committed
为了解决此问题,可以在sendRedirect() / forward() 之后加上 return;
protected void doPost() { if (someCondition) { sendRedirect(); return; } forward();}或者把后一个forward() 放入else中
protected void doPost() { if (someCondition) { sendRedirect(); }else{ forward(); } }上文所述是引起该异常的最常见的情况;近日在一个由服务器下载文件函数中避免了上述问题,但仍然出现了此异常。除去冗余部分的函数如下:
public void download(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... OutputStream out = response.getOutputStream(); try { ... out.write(); } catch (Exception e){ log.error(e.getMessage()); } finally { if (out!=null ){ out.flush(); out.close(); } ... forward(); }Server端在提交response到Client端之前会向一个缓冲区写入相应头和状态码,然后将内容清空。而一旦缓冲区被清空就标志着该response已被提交(response is committed)。于是在finally块中随着out.flush(),该response已被提交了。原因找到,解决方法同上。
相关推荐
NULL 博文链接:https://hw1287789687.iteye.com/blog/1925191
纠结了半天的 java.lang.IllegalStateException: getOutputStream() has already。这个问题困扰了半天,在网上查阅了大量资料 出这个错误一般就是下面2个.....
IllegalStateException: The specified child already has a parent.我的博客中有文章讲解
NULL 博文链接:https://anminer.iteye.com/blog/845067
1、错误信息 异常:Caused by: java.lang....Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.iot.framework.core.response.CommResponse ...
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but...
解决java.lang.IllegalStateException: unread block data的架包
2.java.lang.IllegalStateException: Adapter is detached. 原因: 1.单线程一次执行一个请求可以正常执行,如果使用多线程,同时执行多个请求时就会出现连接超时. 2.HttpConnection没有连接池的概念,多少次请求就...
spring 总的概述。。。入门好教程~~~~~~~~~~~~~
这个存储库提供了一种在处理片段传输和后台任务时避免“java.lang.IllegalStateException:Can not perform this action after onSaveInstanceState”的方法。 您可以在的非常权威的阅读有关该问题和可能的解决方案...
Cause: java.lang.IllegalStateException: Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.:java.lang.IncompatibleClassChangeError: class ...
今天公司springboot项目准备部署到...java.lang.IllegalStateException: 启动子级时出错 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:720) at org.apache.catalina.core.Con
各种数据库在JDBC中的连接方法.及在struts与hibernate中的配置..
SteganoImage 1.0 绿色版
从excel导入数据到datawindow-转载
因工作的缘故,有机会接触了Java本地方法调用(JNI)。本文中记下了初试JNI的过程和一点点体会。 使用Java的JNI调用C/C++的动态联结库有个固定的步骤,下面将以一个最简单的HelloWorld例子程序来说明调用过程。...
GUIDELINES TO DETERMINE IF A DRAWING NEEDS TO BE UPDATED WHEN GD&T IS NOT AFFECTED BY ENGINEERING CHANGE
里面包含了java通过xfire访问webservice用到的jar包,已经打包好了
主要介绍了java 实现websocket的两种方式实例详解,一种使用tomcat的websocket实现,一种使用spring的websocket,本文通过代码给大家介绍的非常详细,需要的朋友可以参考下
Jnative1.4_java调用动态库所需jar,里面包含了一个可以测试的动态库,可以自己拿下来玩玩。