java.lang.IllegalStateException异常产生的原因及解决办法

java.lang.IllegalStateException异常产生的原因及解决办法  

错误类型大致为以下几种:
java.lang.IllegalStateException: Cannot   forward   a   response   that   is   already   committed 
IllegalStateException: response already commited 
IllegalStateException: getOutputStream() has already been called for this request
…………
错误原因:
  该异常表示,当前对客户端的响应已经结束,不能在响应已经结束(或说消亡)后再向客户端(实际上是缓冲区)输出任何内容。

具体分析:

首先解释下flush(),我们知道在使用读写流的时候数据先被读入内存这个缓冲区中, 然后再写入文件,但是当数据读完时不代表数据已经写入文件完毕,因为可能还有一部分仍未写入文件而留在内存中,这时调用flush()方法就会把缓冲区的数据强行清空输出,因此flush()的作用就是保证缓存清空输出。response是服务端对客户端请求的一个响应,其中封装了响应头、状态码、内容等,服务端在把response提交到客户端之前,会向缓冲区内写入响应头和状态码,然后将所有内容flush。这就标志着该次响应已经committed(提交)。对于当前页面中已经committed(提交)的response,就不能再使用这个response向缓冲区写任何东西(注:同一个页面中的response.XXX()是同一个response的不同方法,只要其中一个已经导致了committed,那么其它类似方式的调用都会导致 IllegalStateException异常)。

【注意】能够导致响应已经committed的操作包括:forward, redirect, flushBuffer。
JDK API:

①    flushBuffer
 public void flushBuffer()throws IOException
Forces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written.

②       sendRedirect
 public void sendRedirect(String location)throws IOException
Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

③    forward
public void forward(ServletRequest request,ServletResponse response) throws ServletException,IOException Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.
For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and parameters adjusted to match the path of the target resource.
forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.
The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the ServletRequestWrapper or ServletResponseWrapper classes that wrap them.
备    注:注:在一次响应commit之前,所有的内容输出都将写入servlet引擎的缓冲区(tomcat或weblogic的内容空间), 而在commit之后,上一次response向缓冲区写入的内容,将清空。由于servlet在没有设置单线程的情况下(使用Single-Threaded Model,servlet实现SingleThreadModel接口,jsp使用<%@ page isThreadSafe="false" %>),是多线程的,所以上面所说的缓冲区,都将是该response所属的线程私有的内存空间。有了这个概念,将可以分析碰到的关于servlet多线程的很多问题。如果不能确认response是否已经committed. 可以调用response.isCommitted()来判断。导致这个错误最普遍的原因是,jsp有编译错误。
常见解决办法:

①    response.sendRedirect()方法后加return语句即可,如下:

 response.sendRedirect("login.jsp");

 return;

②    查提交的url是否有误。

③如果你的页面中用了清缓存代码response.flushbuffer();又用到了response.sendRedirect(url);

你可以把response.flushbuffer();去掉,或者用JS的window.location.href="url";来做转向。

④如果你用了OutputStream,而web容器生成的servlet代码中有out.write(””),这个和JSP中调用的
response.getOutputStream()冲突。out.write()这个是字符流,而response.getOutputStream()是字节流,你不能在同一个页面中调用多个输出流。无论先调用哪一个,在调用第二个时都会抛出IllegalStateException,因为在jsp中,out变量是通过response.getWriter得到的。在多个使用了 outputStream的<%%>语句之间不能有空格及多余的字符。也就是页面中除了使用了outputStream的<%%>之外不能有空格或其它任何字符,在之内的语句可以有空格及回车。

在JSP页面做输出的时候有两种方式.一是通过JspWriter,另一个是通过OutputStream,但二者互相排斥.如果并存的话就会报告以上异常.在不得不使用OutputStream的时候.我们必须要把JspWriter舍弃掉了。找到请求异常的页面所对应的Servlet..把其中所有使用JspWriter的语句全部去掉.或者是到你的JSP文件里把动态输出的代码注释掉.这里注意换行和空格制表符均为JspWriter输出.应该一起去掉.保存文件重新启动服务器你会发现上述异常 消失了。
由于jsp container在处理完成请求后会调用releasePageContet方法释放所用的PageContext object,并且同时调用getWriter方法,由于getWriter方法与在jsp页面中使用流相关的getOutputStream方法冲突,所以会造成这种异常,解决办法是:只需要在jsp页面的最后加上两条语句:    (加到</body>之前)

                 out.clear(); 
                 out=pageContext.pushBody();

即可(其中out,pageContext均为jsp内置对象!) 。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
有关struts2下载组件的具体介绍我就在此略过了,看此文的同志想必大多是碰到这样让人很是不爽的问题。 下面就将该问题的最终解决办法介绍如下。 问题概要: 在struts2中使用result里type="stream"的结果类型时,可以实现文件的下载管理,使用时也是比较顺畅,但是当在“下载提示窗口”中点击“取消按钮”时,总是报出“java.lang.IllegalStateException异常异常内容如下: 2011-1-8 20:34:20 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for servlet default threw exception java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407) at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:108) at com.opensymphony.module.sitemesh.filter.PageResponseWrapper.sendError(PageResponseWrapper.java:176) at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:108) at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:770) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:505) at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129) at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:662) 网络解决办法: (虽然该办法可行,但是本人并不提倡。具体原因在之后解释。) 在网络中查询解决办法大多是使用异常处理的办法,其内容如下: <package name="default" extends="struts-default"> <global-results> <result name="client-abort-exception">/ignored.jsp</result> </global-results> </package> <package name="main" extends="struts-default"> <exception-mapping result="client-abort-exception" exception="org.apache.catalina.connector.ClientAbortException"/> <action name="download" class="com.DownloadAction"> <result name="success" type="stream"> <param name="inputName">targetFile</param> <param name="contentDisposition">filename=""</param> <param name="buffersize">4096</param> </result> </action></package> 也就是说,如果抛出了ClientAbortException异常,那就跳转到“ignored.jsp”这个页面,这个页面中什么内容都没有。 还有一种办法是在页面进行try..catch,之后什么都不做 异常原因分析: stream对应的类是org.apache.struts2.dispatcher.StreamResult,该类的处理过程如下: 1。配置其中result标签下的各个参数 2。从服务器中获取输入流,并同时与客户端建立输出流(服务器与客户端链接通过Socket进行连接) 3。当点击“保存”或“打开”时,开始传输数据。如果点击“取消”,关闭所有的流。 这里要注意的是,但是实际发现Socket并没有断开!并且流也没有关闭!这一点非常重要! 所以在JSP容器通过Response获取输出流之前,前面的流并没有关闭,所以会造成该异常的报出。 本文解决办法: 在前面所说的网络解决办法中,使用的是一种躲避的方式解决该问题,也就是置之不理。 这里提供一个办法可以从根本上解决该问题,但是需要下载插件,在附件中有下载,这个插件很小,才4KB而已。 《如果附件下载后文件损坏,可以到http://down.51cto.com/data/158982下载资源,不需要下载豆。》 具体做法如下: 1。将附件解压获取struts2-sunspoter-stream-1.0.jar,并复制在/WEB-INF/lib下 2。在原有的struts.xml的基础上进行相应的配置,配置如下例 <package name="default" namespace="/" extends="struts-default"> <!-- 添加如下内容 --> <result-types> <result-type name="streamx" class="com.sunspoter.lib.web.struts2.dispatcher.StreamResultX"/> </result-types> <action name="download" class="com.DownloadAction"> <!-- type改写 --> <result name="success" type="streamx"> <result name="success" type="streamx"> <!-- 下载文件类型定义 --> <param name="contentType">text/plain</param> <!-- 下载文件处理方法 --> <param name="contentDisposition"> attachment;filename="${downloadChineseFileName}" </param> <!-- 下载文件输出流定义 --> <param name="inputName">downloadFile</param> </result> </action> </package> 在这种方式下,只需添加一个result-type,将原有的result中type改为“streamx”,其他一律不变,在这种情况下,点击“取消”的同时也关闭了流,不会再报出该异常。 之后的执行“取消”后的结果如下:(配置了"log4j.properties"才能看到该结果) 21:23:44,676 WARN StreamResult:45 - StreamResultX Warn : socket write error 如果出现该警告说明正确执行,该警告说明,Socket非正常中断,但是流确实已经关闭,自此再也不用看到上面出现的讨厌异常结果。 转载至 http://sunspot.blog.51cto.com/372554/474983
`java.lang.IllegalStateException` 是一个运行时异常,通常发生在代码尝试在不合适的时间调用某个方法或操作。以下是几种可能导致该异常的情况和解决方法: 1. 操作状态错误:当尝试在未准备好的状态下执行某个操作时,将抛出 `IllegalStateException`。例如,在视图还没有完成布局之前尝试访问视图的大小或位置。解决方法是确保在执行任何操作之前,所有必要的资源都已准备好,如使用 `View.post()` 方法在视图完成布局后再进行操作。 2. 资源已释放:当尝试使用已经被释放的资源或对象时,将抛出 `IllegalStateException`。例如,在使用已经关闭的数据库连接或已经释放的线程池执行任务时,都会导致该异常。解决方法是确保资源在使用之前都处于可用状态,并且在使用后及时释放资源。 3. 状态转换错误:当尝试从一个状态转换到另一个状态时,如果状态转换不合法,则会抛出 `IllegalStateException`。例如,在 `MediaPlayer` 对象还没有准备好时尝试播放音频文件,将抛出该异常。解决方法是确保在进行状态转换之前,所有必要的操作和条件都已满足。 4. 数据格式错误:当尝试使用不正确的数据格式或类型时,将抛出 `IllegalStateException`。例如,在尝试将一个非数字字符串转换为数字时,将抛出该异常。解决方法是确保使用正确的数据格式和类型进行操作,并进行必要的数据类型转换。 总之,要避免 `IllegalStateException` 异常,需要仔细检查代码中所有操作的前提条件和后置条件,并确保在进行任何操作之前,所有必要的资源和条件都已准备好。同时,需要合理地处理异常情况,以避免程序崩溃或出现不可预期的行为。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值