Tomcat6 JSP appears Getoutputstream () has already been called for the cause of this response exception and solution _jsp programming

Source: Internet
Author: User
Tags html tags rand throw exception

1. tomcat6.0 under the JSP Getoutputstream () has already been called for the cause of this response exception and resolution

In the JSP under the tomcat6.0 this error is generally used in the JSP output stream (such as output image verification code, file download, etc.), did not properly handle the reasons for good.

  The specific reason is:
At the end of the function _jspservice (httpservletrequest request, httpservletresponse response), after the JSP is compiled into a servlet in Tomcat, this code

Copy Code code as follows:
finally {
if (_jspxfactory!= null) _jspxfactory.releasepagecontext (_jspx_page_context);
}

This is where the object used in the JSP is freed, and Response.getwriter () is invoked because this method conflicts with Response.getoutputstream ()! So this anomaly will appear.

Then of course to propose solutions, in fact quite simple (not as some friends say)--all the spaces and carriage return symbols in the JSP are deleted--after using the output stream, call the following two lines of code:

Copy Code code as follows:
Out.clear ();
out = Pagecontext.pushbody ();

  Finally, here is an example of an output color verification code (such examples are almost everywhere).

imag.jsp

<%@ page import= "java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%> <%@ page import=
  Java.io.OutputStream "%> <%!
    Color getrandcolor (int fc,int BC) {Random Random = new Random ();
    if (fc>255) fc=255;
    if (bc>255) bc=255;
    int R=fc+random.nextint (BC-FC);
    int G=fc+random.nextint (BC-FC);
    int B=fc+random.nextint (BC-FC);
  return new Color (R,G,B);
    %> <% try{response.setheader ("Pragma", "No-cache");
    Response.setheader ("Cache-control", "No-cache");
    Response.setdateheader ("Expires", 0);
    int width=60, height=20;
    BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);
    OutputStream Os=response.getoutputstream ();
    Graphics g = image.getgraphics ();
    Random Random = new Random ();
    G.setcolor (Getrandcolor (200,250));

    G.fillrect (0, 0, width, height);
    G.setfont (New Font ("Times New Roman", font.plain,18));
    G.setcolor (Getrandcolor (160,200)); for (int i=0;i<155;i+ +) {int x = random.nextint (width);
      int y = random.nextint (height);
      int xl = Random.nextint (12);
      int yl = Random.nextint (12);
     G.drawline (X,y,x+xl,y+yl);
    } String srand= "";
      for (int j=0;j<4;j++) {String rand=string.valueof (Random.nextint (10));
      Srand+=rand;
      G.setcolor (New Color (20+random.nextint), 20+random.nextint (a), 20+random.nextint (110));
    g.DrawString (rand,13*j+6,16);
    } session.setattribute ("Rand", SRand);

    G.dispose ();
    Imageio.write (Image, "JPEG", OS);
    Os.flush ();
    Os.close ();
    Os=null;
    Response.flushbuffer ();
    Out.clear ();
  out = Pagecontext.pushbody ();
    }catch (IllegalStateException e) {System.out.println (E.getmessage ());
  E.printstacktrace ();

 }%>

If there are deficiencies, welcome treatise!

2.getOutputStream () has already been called for this response problem resolution

When the JSP prints the picture to the page, use Response.getoutputstream () will have such a hint: Java.lang.IllegalStateException:getOutputStream () has already Been called for this response will throw exception

  Reason one:
the default output stream for the JSP is PrintWriter, that is, the default output for something other than <%%>, and if you try to use Servletoutputstream in the JSP, it can cause an error. Why not go directly to the servlet output ( Replication Service method, to remove anything except%><% (including HTML tags, spaces, carriage returns, etc.) should be OK. This should be the case for such a situation, delete all the content between the%><% including spaces and line breaks, and finally to eliminate spaces and line breaks, preferably plus a response.reset ().

  Reason two:

In the Java EE API Reference there is this:

  This exception is thrown in the Servletresponse getwriter () method:

Illegalstateexception-if the Getoutputstream method has already been to this called object

  and its Getoutputstream () method throws this exception:

Illegalstateexception-if the Getoutputstream method has already been to this called object

  And both of them have this kind of sentence in their function statement.

Either this and Getoutputstream () May is called to write the body, not both.
Either this and getwriter () May is called to write the body, not both.


  The instructions above also explain why you should use the following looping format when writing pictures to a page

Copy Code code as follows:
OutputStream Output=response.getoutputstream ();
while ((Len=in.read (b)) >0) {
Output.write (B,0,len);
}
Output.flush ();

Instead of putting Response.getoutputstream (). write () into the loop body

Write directly in the page:

Copy Code code as follows:
<body bgcolor= "#ffffff" >
<%
Response.getoutputstream ();
%>
</body>

The error message will appear as follows:

Java.lang.IllegalStateException:getOutputStream () has already been called for this response
Org.apache.catalina.connector.Response.getWriter (response.java:604)
Org.apache.catalina.connector.ResponseFacade.getWriter (responsefacade.java:198)
Org.apache.jasper.runtime.JspWriterImpl.initOut (jspwriterimpl.java:125)
Org.apache.jasper.runtime.JspWriterImpl.flushBuffer (jspwriterimpl.java:118)

The above is Tomcat6 JSP appears Getoutputstream () has already been called for the cause of this response exception and the whole content of the solution, I hope to give you a reference, but also hope that we support cloud habitat community.

Related Article

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.