Java. Lang. illegalstateexception: getoutputstream () has already been called for this response

Source: Internet
Author: User

When I did the graphic verification code, I randomly found a segment of code from the Internet. The result showed this exception. The details are as follows:

 

(
4361578
MS) [HTTP
-
8080
-
Processor25] error: org. Apache. Catalina. Core. containerbase. [Catalina]. [localhost]. [
/
Xixibar]. [JSP] # invoke: servlet. Service ()
For
Servlet JSP threw exception

Java. Lang. illegalstateexception: getoutputstream () has already been called
For
 
This
Response

At org. Apache. Catalina. connector. response. getwriter (response. Java:
599
)

At org. Apache. Catalina. connector. responsefacade. getwriter (responsefacade. Java:
195
)

At org. Apache. Jasper. runtime. jspwriterimpl. initout (jspwriterimpl. Java:
124
)

At org. Apache. Jasper. runtime. jspwriterimpl. flushbuffer (jspwriterimpl. Java:
117
)

At org. Apache. Jasper. runtime. pagecontextimpl. Release (pagecontextimpl. Java:
182
)

At org. Apache. Jasper. runtime. jspfactoryimpl. internalreleasepagecontext (jspfactoryimpl. Java:
115
)

At org. Apache. Jasper. runtime. jspfactoryimpl. releasepagecontext (jspfactoryimpl. Java:
75
)

At org. Apache. jsp. image_jsp. _ jspservice (image_jsp.java:
105
)

At org. Apache. Jasper. runtime. httpjspbase. Service (httpjspbase. Java:
97
)

At javax. servlet. http. httpservlet. Service (httpservlet. Java:
802
)

At org. Apache. Jasper. servlet. jspservletwrapper. Service (jspservletwrapper. Java:
334
)

At org. Apache. Jasper. servlet. jspservlet. servicejspfile (jspservlet. Java:
314
)

At org. Apache. Jasper. servlet. jspservlet. Service (jspservlet. Java:
264
)

At javax. servlet. http. httpservlet. Service (httpservlet. Java:
802
)

At org. Apache. Catalina. Core. applicationfilterchain. internaldofilter (applicationfilterchain. Java:
252
)

At org. Apache. Catalina. Core. applicationfilterchain. dofilter (applicationfilterchain. Java:
173
)

At org. Apache. Catalina. Core. standardwrappervalve. Invoke (standardwrappervalve. Java:
213
)

At org. Apache. Catalina. Core. standardcontextvalve. Invoke (standardcontextvalve. Java:
178
)

At org. Apache. Catalina. Core. standardhostvalve. Invoke (standardhostvalve. Java:
126
)

At org. Apache. Catalina. Valves. errorreportvalve. Invoke (errorreportvalve. Java:
105
)

At org. Apache. Catalina. Core. standardenginevalve. Invoke (standardenginevalve. Java:
107
)

At org. Apache. Catalina. connector. coyoteadapter. Service (coyoteadapter. Java:
148
)

At org. Apache. Coyote. http11.http11processor. Process (http11processor. Java:
869
)

At org. Apache. Coyote. http11.http11baseprotocol $ http11connectionhandler. processconnection (http11baseprotocol. Java:
664
)

At org.apache.tomcat.util.net. pooltcpendpoint. processsocket (pooltcpendpoint. Java:
527
)

At org.apache.tomcat.util.net. leaderfollowerworkerthread. Runit (leaderfollowerworkerthread. Java:
80
)

At org. Apache. tomcat. util. threads. threadpool $ controlrunnable. Run (threadpool. Java:
684
)

At java. Lang. thread. Run (thread. Java:
595
)

I didn't understand what was going on at the beginning. I asked Google for help and found the following explanation:

 

In Tomcat 5, this error occurs in JSP. Generally, the output stream is used in JSP (such as the output image verification code and file download ),

The reason is not properly handled.
Loading data ......


The specific reason is

After JSP is compiled into servlet in Tomcat

There is such a piece of code

Finally
 
...
{


If
(_ Jspxfactory
! =
 
Null
) _ Jspxfactory. releasepagecontext (_ jspx_page_context );

}



The object used in JSP will be released and response. getwriter () will be called because the method is

Response. getoutputstream () conflict! Therefore, the above exception occurs.


Then, of course, we need to propose a solution. In fact, it's quite simple (not just as some friends said
--


Delete all spaces and carriage return characters in JSP ),


Call the following two lines of code after the output stream is used:

Out. Clear ();

Out
=
Pagecontext. pushbody ();

 

Two lines of code are added in this method, and it is OK!

 

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.