java.lang.IllegalStateException: getOutputStream() has already been called for this response


1. Problem Description:

In daily practice, although the page can be displayed smoothly, the background error will be reported: (although it does not affect the export effect, it is still uncomfortable to see the abnormality in the background)

java.lang.IllegalStateException: getOutputStream() has already been called for this response

Semantically, getoutputstream() has called this response, which is the exception caused by repeated calls.

2. Problem analysis:

Under tomcat, the error is usually caused by using the output stream (such as output image captcha, file download, etc.).

The reason for this exception is that there is an exception in the servlet code generated by the web container out.write This is what is called in JSP. response.getOutputStream () conflicts. That is, the servlet specification, can not be called response.getOutputStream (), and then call response.getWriter (), no matter which one is called first, the IllegalStateException should be thrown when calling the second one, because in JSP, the out variable is actually called through the response.getWriter Yes, it’s used in your program response.getOutputStream , and uses the out variable There was a mistake on the page.

3. Solution:

When callingresponse.getOutputStream Before (), clear the contents of the cache, return a new bodycontext, and update the contents of the out attribute of pagecontext.




4. Expansion

The above code does not use out at all. Why can it be called out.clear And pagecontext? Because these are JSP built-in objects, can be used at any time.

Nine built-in objects of JSP (can be used directly on JSP page, but not on HTML page)

4.1 page: a JSP is a servlet class, a Java class. This object of the Java class corresponds to the page object of the JSP;

4.2 pagecontext: current page object

4.3 application: context object

4.4 config: servlet life cycle 1. Initialization

4.5 request: request. Servlet life cycle 2. Service method: request, response

4.6 response

4.7 session: the request corresponds to the session. With the request, you can get the session= request.getSession ();

4.8 out: response corresponds to out object — “this is the root of this exception;

4.9 exception: exception object (if there is code, there will be exception).


Original address:

Thank you for your article

Recommended Today

Use of Android WebView (super detailed usage)

1.1 overview of WebView Android WebView is a special view on the Android platform. It can be used to display web pages. This WebView class can be used to display only one online web page in the app. Of course, it can also be used to develop browsers. The internal implementation of WebView uses WebKit […]