[Subject]: The cause and solution for the getoutputstream () has already been called for this response exception in JSP in Tomcat 5
[Keyword]: tomcat5, JSP, getoutputstream, has, already, been, called, for, this, response
[Laiyuan ]:Http://blog.csdn.net/alexwan/archive/2007/02/13/1508871.aspx
Cause and solution of getoutputstream () has already been called for this response exception in JSP of tomcat5
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.
The specific reason is
After JSP is compiled into servlet in Tomcat
There isCode
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, which is actually quite simple (not just as some friends say --
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 ();
Finally, here is an example of the output color Verification Code (this example is almost everywhere)
Imag. jsp
<% @ Page contenttype = "image/JPEG" Import = "Java. AWT. *, Java. AWT. image. *, Java. util. *, javax. imageIO. *, Java. io. * "%>
<%!
Color getrandcolor (int fc, int BC) {// obtain a random color from a given range
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 {
// Set the page not to cache
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
Outputstream OS = response. getoutputstream ();
// Create an image in memory
Int width = 60, Height = 20;
Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
// Obtain the image Context
Graphics G = image. getgraphics ();
// Generate a random class
Random random = new random ();
// Set the background color
G. setcolor (getrandcolor (200,250 ));
G. fillrect (0, 0, width, height );
// Set the font
G. setfont (new font ("Times New Roman", Font. Plain, 18 ));
// Draw a border
// G. setcolor (new color ());
// G. drawrect (0, 0, width-1, height-1 );
// Generates 155 random interference lines, making the identification code in the image hard to be used by others Program Detected
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 );
}
// Obtain the random ID code (4 digits)
String srand = "";
For (INT I = 0; I <4; I ++ ){
String Rand = string. valueof (random. nextint (10 ));
Srand + = rand;
// Display the authentication code to the image
G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110); // The color from the call function is the same, probably because the seed is too close, so it can only be generated directly.
G. drawstring (RAND, 13 * I + 6, 16 );
}
// Save the authentication code to the session
Session. setattribute ("RAND", srand );
// The image takes effect
G. Dispose ();
// Output the image to the page
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 ();
}
%>