Java.lang.IllegalStateException Exceptions: Simple analysis and simple solutions

Source: Internet
Author: User
Tags throw exception

We may use the output stream of the page when we are doing file upload or download, or filtering.
For example, use in action:
Response.reset ();
Response.setcontenttype ("application/vnd.ms-excel");
OutputStream OS = Response.getoutputstream ();
Throw exception: Java.lang.IllegalStateException


Cause Analysis:
This is a out.write ("") in the servlet code generated by the Web container, which conflicts with the Response.getoutputstream () called in the JSP.
That is, the servlet specification description, cannot call Response.getoutputstream (), and call Response.getwriter (), regardless of which one is called first, should be thrown at the second time of the call IllegalStateException,

Because in the JSP, the out variable is obtained through the Response.getwriter, in the program both uses the Response.getoutputstream, but also uses the out variable, therefore appears above error.

    • Solve:

French one: In the JSP file, add the following two sentences

<%

Out.clear ();
out = Pagecontext.pushbody ();

%>

Defects of this method:
Many development projects are not JSP frontend, such as freemarker,velocity, etc.
The "Response.getoutputstream ()" That caused the problem was not written in the JSP, but in the servlet/action.

Law two: In action, do not return to the specific result file, but return null

return SUCCESS;

return null;

Java.lang.IllegalStateException Exceptions: Simple analysis and simple solutions

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.