Today, I write a java file download program. After it’s finished, everything is normal, but it always throws out java.lang.IllegalStateException Abnormal, although it does not affect the normal use, but it always makes people feel very uncomfortable. There is nothing wrong with checking the code. Finally, I checked a lot of information on the Internet and finally found out the reason.
When we upload or download files, or filter files, we may need to use the output stream of the page.
for example, when we use it in action:
for example, when we use it in action response.reset ();
response.setContentType (”application/ vnd.ms -excel”);
OutputStream os = response.getOutputStream ();
throw an exception: java.lang.IllegalStateException
Cause analysis:
this is a problem in the servlet code generated by the web container out.write (), which is invoked in JSP. response.getOutputStream () conflicts.
that is, the servlet specification states that it cannot be called either response.getOutputStream (), and then call response.getWriter (), no matter which one is called first, an IllegalStateException will be thrown when calling the second one,
Because in JSP, the out variable is generated through the response.getWriter It is used in the program response.getOutputStream , and the out variable is used, so the above error occurs.
solve:
Method 1: add the following two sentences to the JSP file
<% out.clear (); out = pageContext.pushBody (); %> |
Defects of this method:
many development projects are not caused by JSP front-end, such as freemaker, velocity and so on“ response.getOutputStream () “not in JSP, but in servlet/action
Method 2: in action, do not return to the specific result file, return null
instead
//return SUCCESS; return null; |
Read More:
- java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
- java.lang.IllegalStateException: Could not execute method for android:onClick
- Caused by: java.lang.IllegalStateException: Ambiguou There is already ‘XXXXXXController‘ bean method
- Android error java.lang.IllegalStateException : System services not available to Activities before onC
- java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
- Spring boot running appears java.lang.IllegalStateException : Failed to load ApplicationContext
- java.lang.IllegalStateException: Logback configuration error detected
- java.lang.IllegalStateException : unable to read meta data for class
- Idea start error: java.lang.IllegalStateException : failed to create a child event loop
- java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Context
- Realize the simplest recursive call, simulate exception in thread “main” java.lang.stackoverflowerror exception
- Request processing failed;nested exception is java.lang.*
- java.lang.IllegalArgumentException : urlcoder exception resolution
- The solution of a Java exception has occurred. And error exception in thread when eclipse runs
- Method threw ‘java.lang.NullPointerException‘ exception. Cannot evaluate com.sun.proxy.xxx
- Error in idea compilation: java.lang.OutOfMemoryError Java heap space and java.lang.StackOverflowError
- Java:java.lang.OutOfMemoryError : GC overhead limit exceeded solution
- Mybatis exception error setting driver on unpoleddatasource. Cause: java.lang.ClassNotFoundException :
- keytool error: java.lang.Exception: Failed to establish chain from reply
- java.lang.Throwable: Substituted for the exception com.bea.xml.SchemaTypeLoaderException which lack