Turn: Causes and solutions of abnormal java.lang.IllegalStateException

Source: Internet
Author: User
Tags save file

Problem Description:

The error types are roughly the following:

Java.lang.IllegalStateException:Cannot forward a response that's already committed
Illegalstateexception:response already commited
Illegalstateexception:getoutputstream () have already been called for this request

Cause of Error:

The exception indicates that the current response to the client has ended, and that it cannot output anything to the client (actually a buffer) after the response has ended (or died).

Specific analysis: first explain the flush (), we know that when the use of read-write stream data is read into the memory of the buffer, and then write to the file, but when the data read does not mean that the data has been written to the file, because there may still not be a portion of the file to remain in memory, then call Flush () Method will force the data in the buffer to clear the output, so the function of flush () is to ensure that the cache clears the output. Response is a response from the server to a client request that encapsulates the response header, status code, content, and so on, before the server submits the response to the client, writes the response header and status code to the buffer, and then flush all the contents. This indicates that the response has been committed (submitted). For response that have been committed (committed) on the current page, you can no longer use this response to write anything to the buffer (note: response.xxx () on the same page is a different method of response, as long as one has caused committed, other similar calls will result in illegalstateexception exceptions).

"Note" Actions that can cause the response to be committed include: forward, redirect, Flushbuffer.

JDK API:

①flushbuffer

 Public void Flushbuffer ()throws This

②sendredirect

 Public voidSendredirect (String location)throwsIoexceptionsends 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 a 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 was relative with a leading '/'The container interprets it as relative to the servlet container root. If The response has already been committed, ThisMethodthrowsAn illegalstateexception. After using Thismethod, the response should is considered to being committed and should not being written to.

③forward

 Public voidForward (ServletRequest request,servletresponse response)throwsservletexception,ioexceptionforwards a request from a servlet to another resource (servlet, JSP file, or HTML file) o n the server. This method allows one servlet to Dopreliminary processing of a request and another resource to generate the response. For a requestdispatcher obtained via Getrequestdispatcher (), the ServletRequest object have its path elements and parameter  S adjusted to match the path of the target Resource.forward should be called before the response have been committed to the Client (before response body output has been flushed). If The response already has been committed, ThisMethodthrowsAn illegalstateexception.
Uncommitted output in the response buffer is automatically cleared before the forward. The request and response parameters must is either the same objects as were passed to the calling servlet' s service method or is subclasses of the Servletrequestwrapper or servletresponsewrapper classes that wrap them.

Note: All content output will be written to the servlet engine's buffer (tomcat or weblogic content space) before a commit is made, and after commit, the last response writes to the buffer will be emptied. JSP uses <%@ page isthreadsafe= "false"% because the servlet implements the Singlethreadmodel interface with single-threaded Model,servlet without a single thread set >) is multithreaded, so the above-mentioned buffer will be the private memory space of the thread to which the response belongs. With this concept, you will be able to analyze many of the problems encountered with the servlet multi-Threading. If you cannot confirm whether the response has been committed. Can call response.iscommitted () to judge. The most common cause of this error is that the JSP has a compilation error.

Common solutions:

① after the Response.sendredirect () method, add the return statement, as follows: Response.sendredirect ("login.jsp"); Return

② Check that the submitted URL is incorrect.

③ If your page uses a clear cache code Response.flushbuffer (), and the use of response.sendredirect (URL); You can turn response.flushbuffer (), remove, or use JS's window.location.href= "url";

④ If you use OutputStream, and the Web container generates Out.write ("") in the servlet code, this conflicts with the Response.getoutputstream () called in the JSP.

Out.write () This is a character stream, and Response.getoutputstream () is a byte stream, and you cannot invoke multiple output streams in the same page. Whichever is called first, IllegalStateException is thrown when the second one is called.

Because in the JSP, the out variable is obtained through Response.getwriter. There can be no spaces and extra characters between multiple <%%> statements that use OutputStream.

In other words, there can be no spaces or any other characters in the page except for the use of the OutputStream <%%>, within which the statements may have spaces and carriage returns.

There are two ways to do output on a JSP page. One is through JspWriter, the other is through OutputStream, but they are mutually exclusive. If coexisting, the above exception is reported. When we have to use outputstream. We have to abandon JspWriter.

Locate the servlet that corresponds to the page that requested the exception: Remove all statements that use JspWriter. Or to comment out the dynamic output code in your JSP file.

Note that newline and Space tabs are jspwriter output. should be removed together. save file Restart server you will notice that the above exception disappears.

Because JSP container calls the Releasepagecontet method to release the PageContext object used after processing the completion request, and calls the Getwriter method at the same time,

Because the Getwriter method conflicts with the flow-related Getoutputstream method in the JSP page, this exception is caused.

The workaround is: Just add two statements at the end of the JSP page: Out.clear (); Out=pagecontext.pushbody (); (where Out,pagecontext are JSP built-in objects!).

Turn: Causes and solutions of abnormal java.lang.IllegalStateException

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.