关于java.lang.illegalstateexception的信息

发布时间:2022-11-13

本文目录一览:

  1. java.lang.IllegalStateException异常是什么问题?
  2. java.lang.IllegalStateException怎么解决
  3. java.lang.IllegalStateException异常是什么问题
  4. java.lang.IllegalStateException是什么错误?
  5. 为什么会出现“java.lang.illegalstateexception”?

java.lang.IllegalStateException异常是什么问题?

错误原因:
该异常表示,当前对客户端的响应已经结束,不能在响应已经结束(或说消亡)后再向客户端(实际上是缓冲区)输出任何内容。 在一次响应 commit 之前,所有的内容输出都将写入 servlet 引擎的缓冲区(如 Tomcat 或 WebLogic 的内容空间),而在 commit 之后,上一次 response 向缓冲区写入的内容将被清空。由于 servlet 在没有设置单线程的情况下(使用 Single-Threaded Model,servlet 实现 SingleThreadModel 接口,JSP 使用 <%@ page isThreadSafe="false" %>)是多线程的,所以上面所说的缓冲区,都将是该 response 所属的线程私有的内存空间。有了这个概念,将可以分析碰到的关于 servlet 多线程的很多问题。如果不能确认 response 是否已经 committed,可以调用 response.isCommitted() 来判断。导致这个错误最普遍的原因是,JSP 有编译错误。

java.lang.IllegalStateException怎么解决

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

  1. response.sendRedirect() 方法后加 return 语句即可,如下:
    response.sendRedirect("login.jsp");
    return;
    
  2. 检查提交的 URL 是否有误。
  3. 如果你的页面中用了清缓存代码 response.flushBuffer(); 又用到了 response.sendRedirect(url);,你可以把 response.flushBuffer(); 去掉,或者用 JS 的 window.location.href="url"; 来做转向。
  4. 如果你用了 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 容器在处理完成请求后会调用 releasePageContext 方法释放所用的 PageContext 对象,并且同时调用 getWriter 方法,由于 getWriter 方法与在 JSP 页面中使用流相关的 getOutputStream 方法冲突,所以会造成这种异常。解决办法是:只需要在 JSP 页面的最后加上两条语句:
    out.clear();
    out = pageContext.pushBody();
    
    即可(其中 outpageContext 均为 JSP 内置对象)。

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 的操作包括:forwardredirectflushBuffer

JDK API

  1. 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.
  2. 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.
  3. 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 有编译错误。 常见解决办法:
  4. response.sendRedirect() 方法后加 return 语句即可,如下:
    response.sendRedirect("login.jsp");
    return;
    
  5. 检查提交的 URL 是否有误。
  6. 如果你的页面中用了清缓存代码 response.flushBuffer(); 又用到了 response.sendRedirect(url);,你可以把 response.flushBuffer(); 去掉,或者用 JS 的 window.location.href="url"; 来做转向。
  7. 如果你用了 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 容器在处理完成请求后会调用 releasePageContext 方法释放所用的 PageContext 对象,并且同时调用 getWriter 方法,由于 getWriter 方法与在 JSP 页面中使用流相关的 getOutputStream 方法冲突,所以会造成这种异常。解决办法是:只需要在 JSP 页面的最后加上两条语句:
    out.clear();
    out = pageContext.pushBody();
    
    即可(其中 outpageContext 均为 JSP 内置对象)。

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 异常)。

为什么会出现“java.lang.illegalstateexception”?

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