Causes and solutions of abnormal java.lang.IllegalStateException

Source: Internet
Author: User

cause of error: the exception indicates that the current response to the client has ended, and the response has ended (or died) before the client (in effect, the buffer) outputs any content.

Specific analysis:

first explain flush (), we know that when using read-write stream, the data is read into the memory buffer, the file is then written, but when the data is read, it does not mean that the data has been written to the file, because there may be part is still not written to the file and is left in memory, calling the flush () method will force the data in the buffer clears the output, so flush () is the function of ensuring that the cache empties 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 content. This indicates that the response has been committed (submitted). For response that are already committed (committed) on the current page, you can no longer use this response to write anything to the buffer .

(Note: response in the same page. ) XXX () is a different method of the same response, as long as one of them has caused the committed, then other similar calls will result in a IllegalStateException exception).

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

JDK API:

Flushbuffer

   Public void Flushbuffer ()throws IOException
Forces any content in the buffer to being written to the
client. A call to this method automatically commits the response, meaning the status code and headers would 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 a absolute URL before sending the response to the client. If the location was 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 have already been committed, this method throws an illegalstateexception. After using this method, the response should is considered to being committed and should not being written to.          

forward
 Public void 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 servlets to does 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 have been committed to the client (before response body output have been flushed). If The response already have 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 is either the same objects as were passed to the calling Servlet ' s service method The or is subclasses ServletRequestWrapper of the or ServletResponseWrapper classes that wrap them.

 

Notes

Note: All content output is written to the servlet engine's buffer (tomcat or

WebLogic content Space), and after commit, the last time response writes to the buffer, it will be emptied.

Because the servlet does not have a single thread set ( using the single-threaded model,servlet implementation

Singlethreadmodel interface, JSP uses <%@ page isthreadsafe= "false"%>), is multithreaded, so

The buffer described above 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:

① add a return statement after the Response.sendredirect () method, 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 a servlet code that has Out.write (""), this and the one called in the JSP

Response.getoutputstream () conflict. Out.write () This is a character stream, while Response.getoutputstream ()

is a byte stream, you cannot invoke multiple output streams in the same page. Whichever is called first, it will be thrown when the second one is called

IllegalStateException, because in JSPs, out variables are obtained through response.getwriter. In multiple use of the

There must be no spaces and extra characters between the OutputStream <%%> statements. In addition to the use of the page

OutputStream cannot have spaces or any other characters outside of the <%%>, within which statements can have spaces and carriage returns.

There are two ways to do output on a JSP page. One is through JspWriter and 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. Found it

the page requesting the exception The corresponding servlet: Remove all statements that use JspWriter.

or to put it in your JSP file . the code for the dynamic output is commented out. Note The newline and Space tab are

output for JspWriter. should be removed together. Save file Restart server you will find the above exception

disappeared.                     

 
         Because JSP container calls the Releasepagecontet method after processing the completion request to release
The PageContext object is used, and the Getwriter method is          called at the same time , because the Getwriter method
         This exception is caused by a conflict with the Getoutputstream method used in the JSP page for flow-related methods.
         Solve
Out=pagecontext.pushbody ();
(where Out,pagecontext are JSP built-in objects!).

Causes and solutions of abnormal java.lang.IllegalStateException

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.