java.lang.IllegalStateException错误原因以及解决方法

1 篇文章 0 订阅
1 篇文章 0 订阅
Servlet.service() for servlet default threw exception
java.lang.IllegalStateException
    at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
    at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:725)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:483)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at filters.TomcatFormFilter_UTF8.doFilter(TomcatFormFilter_UTF8.java:79)
    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)

错误原因

 该异常表示,当前对客户端的响应已经结束,不能在响应已经结束(或说消亡)后再向客户端(实际上是缓冲区)输出任何内容。

这是web容器生成的servlet代码中有out.write(””),这个和JSP中调用的response.getOutputStream()产生冲突.即Servlet规范说明,不能既调用 response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个,在调用第二个时候应会抛出 IllegalStateException,因为在jsp中,out变量是通过response.getWriter得到的,在程序中既用了response.getOutputStream,又用了out变量,故出现以上错误.

具体分析

 首先解释下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有编译错误。

解决:

法一:在JSP文件中,加入下面两句

<%
out.clear();
out = pageContext.pushBody();
%>

其中out,pageContext均为jsp内置对象!

此法的缺陷:
很多开发项目并不是JSP前端,如freemarker,velocity等
造成问题的"response.getOutputStream()"并未被写在JSP里,而是写在servlet/action里.

法二: 在struts2的action中,不要return 回具体的result文件,而是return null

//return SUCCESS;
 return null;


  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java.lang.IllegalStateException: COMPLETED 是Java中的一个异常类型,表示当前操作在已经完成的状态下被调用,这通常是由于程序逻辑错误或者不正确的调用顺序导致的。要解决这个问题,你可以考虑以下几个方面: 1. 检查代码逻辑:首先,你需要仔细检查代码逻辑,确保在调用相关方法之前,没有提前完成或结束相关操作。这可能涉及到对方法调用顺序、条件判断和循环等进行仔细检查。 2. 检查并发操作:如果你的代码涉及到多线程或并发操作,那么可能是由于线程间的竞争条件导致了该异常。你可以考虑使用同步机制(如锁、信号量等)来保证线程安全性,或者使用并发工具类(如Atomic类、ConcurrentHashMap等)来避免竞争条件。 3. 检查资源释放:有时候,该异常可能是由于资源没有正确释放导致的。比如,在使用IO流或数据库连接时,需要确保在使用完毕后及时关闭相关资源。你可以使用try-with-resources语句或者手动关闭资源来避免该异常。 4. 查看相关文档和日志:如果以上方法都没有解决问题,你可以查看相关的文档和日志,了解该异常的具体上下文和原因。这可能需要查看相关库或框架的官方文档,或者查看程序的日志文件,以便更好地理解和解决问题。 希望以上方法能够帮助你解决 java.lang.IllegalStateException: COMPLETED 异常。如果你有任何进一步的问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值