Java. Lang. illegalstateexception: simple analysis and solutions

Source: Internet
Author: User
When uploading, downloading, or filtering files, we may need to use the output stream of the page.
For example, in JSP:
Response. Reset ();
Response. setcontenttype ("application/vnd. MS-excel ");
Outputstream OS = response. getoutputstream ();
Throw an exception:
Error [engine] standardwrappervalve [JSP]: servlet. Service () for servlet JSP threw exception
Java. Lang. illegalstateexception: getoutputstream () has already been called for this response
I found the following information from the Internet and analyzed the cause:
This is because the servlet code generated by the Web container contains out. Write ("), which conflicts with response. getoutputstream () called in JSP.
That is, the servlet specification description cannot call either response. getoutputstream () and call response. getwriter (), no matter which one is called first, the illegalstateexception should be thrown during the second call, because in JSP, The out variable is through response. getwriter uses response in the program. the above error occurs because the out variable is used again in getoutputstream.
Solution:
1. Add in the program:
Out. Clear ();
Out = pagecontext. pushbody ();
You can;

2. Do not include spaces or line breaks between %] [%
3. Flush () is required when writing images to the page ()
Outputstream output = response. getoutputstream ();
Output. Flush ();
4. Write <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
Sun weiqin said it may be a tomcat bug. I sent a letter to her:

I looked at it here,

Http://www.javathinker.org/main.jsp? BC = showessay. JSP + filename = tomcat/tomcat_question_chapter13.htm

Here is a post you reply to. The points in the post are basically the same as what I understand. However, it may be a bug in Tomcat. I would like to explain:

In JSP, out is an embedded object, that is, printwriter out = response. getwriter (); in this way, when getoutputstream () again gets the output stream (for example, when forwarding filtering and downloading files), an error occurs (write rejection lock ), I see more than once that someone's file download page keeps printing this exception in the background.

The servlet does not have the default out Built-in object, so there is no error.

You can try to add an out object in the servlet. An exception is reported.

Therefore, the correct processing method should be: Make a control layer in the servlet, and do not get out objects before business processing, the out object echo operation result is generated when the business operation fails or an exception occurs.

You can do this:
Try... {...} catch (exception e )...{

Printwriter out = response. getwriter ();

Out. println ("[Script Type =" text/JavaScript "] Alert ('your attachment cannot be downloaded normally, please check the operation or contact the postmaster '); window. close (); [/script] ");

}



Thank you for your advice.
 

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.