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

2023-05-16

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;


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java.lang.IllegalStateException错误原因以及解决方法 的相关文章

  • 可序列化对象的 ArrayList 的加密保存和解密加载

    我在 SD 卡中保存并加载一个文件 其中包含ArrayList使用这两种方法的可序列化对象 保存方法 public static void saveUserList ArrayList
  • Java 比 Xmx 参数消耗更多内存

    我有一个非常简单的 Web 服务器类 基于 Java SEHttpServer class 当我使用此命令启动编译的类来限制内存使用时 java Xmx5m Xss5m Xrs Xint Xbatch Test 现在如果我使用检查内存top
  • 调试器不会停止在 Intellij IDEA 中的源代码处

    我有一个相当奇怪的问题 无法使用 Intellij IDEA 解决 我正在解析电子邮件文件org apache james mime4j包裹 但我的邮件文件格式不兼容Date 标头 因此 我从 mime4j 源创建了模块 并从磁盘中删除了
  • 图像在 3D 空间中绕 Y 轴旋转

    我有一个 BufferedImage 我想用 theta 角而不是仿射变换绕 Java 中的 Y 轴旋转图像 图片 旋转将如下图所示 矩形将是图像 我可以通过旋转图像的每个像素并绘制图像来做到这一点 因为我必须旋转很多图像 所以我认为这不是
  • 如何确定 JDialog 显示在哪个屏幕上

    我有一个非常大的应用程序 有多个对话框 我的任务是确保不完全可见的对话框 因为用户将其从可见屏幕区域拉出 移回屏幕中心 当我只处理一个屏幕时 这没问题 它工作得很好 但是 该应用程序的大多数用户的桌面上都有两个屏幕 当我尝试找出对话框显示在
  • java springrabbit - 优雅地拒绝消息

    我有以下侦听器方法 Override public void onMessage Message message Channel channel try do something bad catch Exception e try long
  • 如何在不冒 StackOverflowError 风险的情况下使用 CompletableFuture?

    我想遍历异步函数的搜索空间 我将逻辑编码如下 Assuming that a function maps a range of inputs to the same output value minimizes the input valu
  • java内存不足然后退出

    我有一个必须分析大文件的软件 限制输入或提供无限内存都不是一个选择 所以我必须忍受飞行的 OOME 因为 OOME 只杀死线程 所以我的软件运行在一些糟糕的状态 从外面看一切都很好 因为进程正在运行 但在内部却是脑死亡 我想拔掉它的插头 但
  • 错误包括 bouncycastle 提供商

    我需要使用bouncycastle provider我的项目中的库 我已将其包含在 gradle 项目中 apply plugin application sourceCompatibility 1 6 version 1 0 0 main
  • jasper 报告文件中出现错误

    首先 我在 iReport 5 1 0 中创建一个 R D1 jrxml 文件 我执行该报告的 Java 代码如下所示 import java sql Connection import java sql DriverManager imp
  • 致命异常:OkHttp 调度程序

    我在 Android 应用程序中使用 OkHttp 库向天气 API 发出 Web 请求 我已经实现了我的代码 但在执行请求时遇到了致命异常 我也已经在我的清单中添加了互联网权限 MainActivity java private Curr
  • java.lang.IllegalStateException:FragmentManager 已被销毁

    活动中onResume我称之为 volley request 的方法 它获取项目列表 然后将它们加载到此活动内的 ListFragment 中 当我第一次进入活动时 一切正常 但当我重新进入活动时 ListFragment 为空 并且控制台
  • 如何预先填充 JFileChooser 将“文件名”?

    我打算用数据库中的名称填充 JFileChooser 但使用标准 JFileChooser 对话框进行加载 删除 保存和另存为 我想给用户留下这样的印象 他们正在处理文件系统 而在后端使用数据库来保存更改 用户不应该能够浏览到不同的目录进行
  • Java 中有类似 .NET 的 NotImplementedException 的东西吗?

    有没有类似 NET 的东西NotImplementedException在Java中 康芒斯朗 http commons apache org proper commons lang javadocs api 2 6 org apache
  • 使用替换但不使用根元素的 Jaxb 继承

    我正在浏览布莱斯的博客http blog bdoughan com 2010 11 jaxb and inheritance using substitution html http blog bdoughan com 2010 11 ja
  • CreateProcess error=206,运行 gwtCompile 时文件名或扩展名太长

    我的应用程序是一个 springboot gradle 应用程序 我的应用程序的一部分涉及使用 gradle 进行遗留 gwt 编译 它工作正常 但今天当我运行下面的 gradle 任务时 它显示 CreateProcess error 2
  • 当框架被拖动时,如何设置 JWindow 的位置位于文本字段下方?

    我正在制作一个自动完成项目 就像谷歌一样 我的框架中有一个 jtextfield 每当我在该字段中输入内容时 该文本字段下方就会出现一个 JWindow 并且该窗口来自另一个类 现在的问题是 每当我拖动框架时 如何使窗口始终出现在文本字段下
  • 如何使用 iBatis (myBatis) 的注释进行 IN 查询?

    我们只想在 MyBatis 中使用注释 我们确实在努力避免使用 xml 我们尝试使用 IN 子句 Select SELECT FROM blog WHERE id IN ids List
  • Android NDK - 仅用 C/C++ 编写

    有没有一种可能的方法可以使用 C C 编写整个 NDK 应用程序 而无需像 hello jni 示例项目 HelloJni java 中那样的 Java 入门 类 以某种方式创建一个 HelloJni c 来执行相同的操作 从 Androi
  • Spring Boot 2 中的 401 代替 403

    With 春季启动 https projects spring io spring boot 1 5 6 发布我能够发送 HTTP 状态代码401代替403如中所述如果请求未经身份验证的uri 如何让Spring Security响应未经授

随机推荐