Cause and solution for the getOutputStream () has already been called for this response exception in jsp at Tomcat 6, havealreadybeen

Source: Internet
Author: User

Cause and solution for the getOutputStream () has already been called for this response exception in jsp at Tomcat 6, havealreadybeen

1. Cause and solution of getOutputStream () has already been called for this response exception in jsp under tomcat6.0

In tomcat6.0 jsp, this error is generally caused by the use of the output stream (such as the output image verification code, file download, etc.) in jsp, which is not properly handled.

The specific reason is:
In tomcat, after jsp is compiled into servlet, there is such code at the end of the function _ jspService (HttpServletRequest request, HttpServletResponse response ).

Copy codeThe Code is as follows: finally {
If (_ jspxFactory! = Null) _ jspxFactory. releasePageContext (_ jspx_page_context );
}

The object used in jsp is released and response. getWriter () is called because this method conflicts with response. getOutputStream! Therefore, the above exception occurs.

Then, of course, we need to propose a solution. In fact, it is quite simple (not 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:

Copy codeThe Code is as follows: out. clear ();
Out = pageContext. pushBody ();

  Finally, here is an example of the output color Verification Code (this example is 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(110),20+random.nextInt(110),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 you have any shortcomings, please feel free to make an axe!

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

When you use response. getOutputStream () to output images to the page, the following message is displayed: java. lang. IllegalStateException: getOutputStream () has already been called for this response. Exception is thrown.

  Cause 1:
The default output stream of JSP is PrintWriter, that is, the default output mode of anything other than <%>. If you try to use ServletOutputStream in JSP, it will cause an error. if you want to directly use Servlet output (rewrite service method), you can delete anything except %> <% (including HTML tags, spaces, and carriage returns. In this case, all content between %> <% should be deleted, including spaces and line breaks. At last, spaces and line breaks should be eliminated. It is best to add a response. reset ().

  Cause 2:

In the J2EE API reference, there is such:

This exception is thrown in the getWriter () method of ServletResponse:

IllegalStateException-if the getOutputStream method has already been called for this response object

The getOutputStream () method throws this exception:

IllegalStateException-if the getOutputStream method has already been called for this response object

And there is such a sentence in both function declarations.

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


  The preceding description also explains why the following circular format is used when writing images to the page.

Copy codeThe Code is as follows: OutputStream output = response. getOutputStream ();
While (len = in. read (B)> 0 ){
Output. write (B, 0, len );
}
Output. flush ();
Instead of placing response. getOutputStream (). write () in the loop body

Directly write on the page:
Copy codeThe Code is as follows: <body bgcolor = "# ffffff">
<H1>
<%
Response. getOutputStream ();
%>
</H1>
</Body>

The following error message is displayed:

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 the reason why the getOutputStream () has already been called for this response exception occurs in the jsp of Tomcat 6 and all the solutions. I hope you can give us a reference and support the help house.

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.