Getoutputstream () has already been called for this

Source: Internet
Author: User
<%
Bufferedimage image = new bufferedimage (400,400, bufferedimage. type_int_rgb );
Graphics G = image. getgraphics ();
G. fillrect (0, 0, 400,400 );
G. setcolor (new color (255, 0, 0 ));
G. fillarc (100,100, 30,120 );
G. setcolor (new color (0,255, 0 ));
G. fillarc (20,20, 100,100,150, 20 );
G. setcolor (new color (0, 0, 255 ));
G. fillarc (20, 20, 100,100,270,120 );
G. setcolor (new color (0, 0 ));
G. drawstring ("RED: Climb", 300, 80 );
G. drawstring ("Green: Swim", 300,120 );
G. drawstring ("blue: Jump", 300,160 );
ImageIO. Write (image, "BMP", response. getoutputstream ());
// Out. Clear ();
// Out = pagecontext. pushbody ();
%>


The following exception is thrown during Tomcat running:


At org. Apache. Catalina. connector. response. getwriter (response. Java: 601)
At org. Apache. Catalina. connector. responsefacade. getwriter (responsefacade. Java: 196)
At org. Apache. Jasper. runtime. jspwriterimpl. initout (jspwriterimpl. Java: 125)
At org. Apache. Jasper. runtime. jspwriterimpl. flushbuffer (jspwriterimpl. Java: 118)
At org. Apache. Jasper. runtime. pagecontextimpl. Release (pagecontextimpl. Java: 185)
At org. Apache. Jasper. runtime. jspfactoryimpl. internalreleasepagecontext (jspfactoryimpl. Java: 116)
At org. Apache. Jasper. runtime. jspfactoryimpl. releasepagecontext (jspfactoryimpl. Java: 76)
At org. Apache. jsp. pages. drawimage_jsp. _ jspservice (drawimage_jsp.java: 84)
At org. Apache. Jasper. runtime. httpjspbase. Service (httpjspbase. Java: 98)
At javax. servlet. http. httpservlet. Service (httpservlet. Java: 803)
At org. Apache. Jasper. servlet. jspservletwrapper. Service (jspservletwrapper. Java: 328)
At org. Apache. Jasper. servlet. jspservlet. servicejspfile (jspservlet. Java: 315)
At org. Apache. Jasper. servlet. jspservlet. Service (jspservlet. Java: 265)
At javax. servlet. http. httpservlet. Service (httpservlet. Java: 803)
At org. Apache. Catalina. Core. applicationfilterchain. internaldofilter (applicationfilterchain. Java: 269)
At org. Apache. Catalina. Core. applicationfilterchain. dofilter (applicationfilterchain. Java: 188)
At org. Apache. Catalina. Core. standardwrappervalve. Invoke (standardwrappervalve. Java: 210)
At org. Apache. Catalina. Core. standardcontextvalve. Invoke (standardcontextvalve. Java: 174)
At org. Apache. Catalina. Core. standardhostvalve. Invoke (standardhostvalve. Java: 127)
At org. Apache. Catalina. Valves. errorreportvalve. Invoke (errorreportvalve. Java: 117)
At org. Apache. Catalina. Core. standardenginevalve. Invoke (standardenginevalve. Java: 108)
At org. Apache. Catalina. connector. coyoteadapter. Service (coyoteadapter. Java: 151)
At org. Apache. Coyote. http11.http11processor. Process (http11processor. Java: 870)
At org. Apache. Coyote. http11.http11baseprotocol $ http11connectionhandler. processconnection (http11baseprotocol. Java: 665)
At org.apache.tomcat.util.net. pooltcpendpoint. processsocket (pooltcpendpoint. Java: 528)
At org.apache.tomcat.util.net. leaderfollowerworkerthread. Runit (leaderfollowerworkerthread. Java: 81)
At org. Apache. tomcat. util. threads. threadpool $ controlrunnable. Run (threadpool. Java: 685)
At java. Lang. thread. Run (thread. Java: 595)


View the converted JSP code and find the following 84th lines (highlighted in green ):

Public void _ jspservice (httpservletrequest request, httpservletresponse response)
Throws java. Io. ioexception, servletexception {

Jspfactory _ jspxfactory = NULL;
Pagecontext = NULL;
Httpsession session = NULL;
Servletcontext application = NULL;
Servletconfig Config = NULL;
Jspwriter out = NULL;
Object page = this;
Jspwriter _ jspx_out = NULL;
Pagecontext _ jspx_page_context = NULL;


Try {
_ Jspxfactory = jspfactory. getdefafactory Factory ();
Response. setcontenttype ("text/html; charset = UTF-8 ");
Pagecontext = _ jspxfactory. getpagecontext (this, request, response,
Null, true, 8192, true );
_ Jspx_page_context = pagecontext;
Application = pagecontext. getservletcontext ();
Config = pagecontext. getservletconfig ();
Session = pagecontext. getsession ();
Out = pagecontext. getout ();
_ Jspx_out = out;

Out. Write ("\ r \ n ");
Out. Write ("\ n ");
Out. Write ("<! Doctype HTML public \ "-// W3C // dtd html 4.01 transitional // en \" \ "http://www.w3.org/tr/html4/loose.dtd\"> \ n ");
Out. Write ("<HTML> \ n ");
Out. Write ("Out. Write ("<meta http-equiv = \" Content-Type \ "content = \" text/html; charsets = UTF-8 \ "> \ n ");
Out. Write ("<title> draw image </title> \ n ");
Out. Write ("Out. Write ("<body> \ n ");
Out. Write ("\ r \ n ");

Bufferedimage image = new bufferedimage (400,400, bufferedimage. type_int_rgb );
Graphics G = image. getgraphics ();
G. fillrect (0, 0, 400,400 );
G. setcolor (new color (255, 0, 0 ));
G. fillarc (100,100, 30,120 );
G. setcolor (new color (0,255, 0 ));
G. fillarc (20,20, 100,100,150, 20 );
G. setcolor (new color (0, 0, 255 ));
G. fillarc (20, 20, 100,100,270,120 );
G. setcolor (new color (0, 0 ));
G. drawstring ("RED: Climb", 300, 80 );
G. drawstring ("Green: Swim", 300,120 );
G. drawstring ("blue: Jump", 300,160 );
ImageIO. Write (image, "BMP", response. getoutputstream ());
// Out. Clear ();
// Out = pagecontext. pushbody ();

Out. Write ("\ r \ n ");
Out. Write ("</body> \ n ");
Out. Write ("} Catch (throwable t ){
If (! (T instanceof skippageexception )){
Out = _ jspx_out;
If (OUT! = NULL & out. getbuffersize ()! = 0)
Out. clearbuffer ();
If (_ jspx_page_context! = NULL) _ jspx_page_context.handlepageexception (t );
}
} Finally {
If (_ jspxfactory! = NULL) _ jspxfactory. releasepagecontext (_ jspx_page_context );
}
}


We can see that when resources are released on the JSP page, the servetresponse. getwriter () method is called, and then the program throws an exception. Check the servlet API to find the problem:

Public java. Io. printwriter getwriter ()
Throws java. Io. ioexception

Returns a printwriter object that can send character text to the client. the printwriter uses the character encoding returned by getcharacterencoding (). if the response's character encoding has not been specified as described in getcharacterencoding (I. E ., the method just returns the default value ISO-8859-1), getwriter updates it to ISO-8859-1.

Calling flush () on the printwriter commits the response.

Either this method or getoutputstream () may be called to write the body, not both.



Returns:

A printwriter object that can return character data to the client

Throws:

Unsupportedencodingexception-if the character encoding returned by getcharacterencoding cannot be used

Java. Lang. illegalstateexception-if the getoutputstream method has already been called for this response object

Java. Io. ioexception-if an input or output exception occurred

See also:

Getoutputstream (), setcharacterencoding (Java. Lang. String)

As the API says, because servletresponse. the getoutputstream () method and the method can be called to output the content of the JSP page. If one of the methods is called, an exception is thrown when another method is called.

The solution is as follows:

Remove the comments of the last two lines of code on the JSP page. The functions of these two lines of code are as follows:

Out. Clear (): clears the cached content.

Pagecontext. pushbody (): reference API

Public bodycontent pushbody ()

Return a new bodycontent object, save the current "out" jspwriter, and update the value of the "out" attribute in the page scope attribute namespace of the pagecontext.



Returns:

The new bodycontent

· Return a new bodycontent (representing the body content of an HTML page)
· Save the out object of the jspwriter instance
· Update the content of the out attribute of pagecontext

Getoutputstream () has already been called for this

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.