天天看點

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

jsp出現getoutputstream() has already been called for this response異常的原因和解決方法

在tomcat5下jsp中出現此錯誤一般都是在jsp中使用了輸出流(如輸出圖檔驗證碼,檔案下載下傳等),

沒有妥善處理好的原因。

具體的原因就是

在tomcat中jsp編譯成servlet之後在函數_jspservice(httpservletrequest request, httpservletresponse response)的最後

有一段這樣的代碼

finally {

      if (_jspxfactory != null) _jspxfactory.releasepagecontext(_jspx_page_context);

    }

這裡是在釋放在jsp中使用的對象,會調用response.getwriter(),因為這個方法是和

response.getoutputstream()相沖突的!是以會出現以上這個異常。

然後當然是要提出解決的辦法,其實挺簡單的(并不是和某些朋友說的那樣--

将jsp内的所有空格和回車符号所有都删除掉),

在使用完輸出流以後調用以下兩行代碼即可:

out.clear();

out = pagecontext.pushbody();

最後這裡是一個輸出彩色驗證碼例子(這樣的例子幾乎随處可見)

imag.jsp

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

<%@ page contenttype="text/html;charset=gb2312" %>

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

<%@ page import="java.awt.*" %>

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

<%@ page import="java.awt.image.*" %>

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

<%@ page import="java.util.*" %>

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

<%@ page import="javax.imageio.*" %>

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

<%!

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

color getrandcolor(int fc,int bc)...{//給定範圍獲得随機顔色

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

random random = new random();

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

if(fc>255) fc=255;

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

if(bc>255) bc=255;

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

int r=fc+random.nextint(bc-fc);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

int g=fc+random.nextint(bc-fc);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

int b=fc+random.nextint(bc-fc);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

return new color(r,g,b);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

}

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

%>

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

<%

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

// 在記憶體中建立圖象

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

int width=80, height=20;

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

// 擷取圖形上下文

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

graphics g = image.getgraphics();

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

//生成随機類

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

// 設定背景色

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

g.setcolor(getrandcolor(200,250));

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

g.fillrect(0, 0, width, height);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

//設定字型

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

g.setfont(new font("times new roman",font.plain,18));

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

//畫邊框

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

//g.setcolor(new color());

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

//g.drawrect(0,0,width-1,height-1);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

// 随機産生155條幹擾線,使圖象中的認證碼不易被其它程式探測到

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

g.setcolor(getrandcolor(160,200));

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

for (int i=0;i<155;i++)

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

...{

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

int x = random.nextint(width);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

int y = random.nextint(height);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

int xl = random.nextint(12);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

int yl = random.nextint(12);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

g.drawline(x,y,x+xl,y+yl);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

// 取随機産生的認證碼(5位數字)

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

string srand="";

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

for (int i=0;i<5;i++)...{

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

string rand=string.valueof(random.nextint(10));

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

srand+=rand;

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

// 将認證碼顯示到圖象中

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

g.setcolor(new color(20+random.nextint(110),20+random.nextint(110),20+random.nextint(110)));//調用函數出來的顔色相同,可能是因為種子太接近,是以隻能直接生成

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

g.drawstring(rand,13*i+6,16);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

// 将認證碼存入session

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

session.setattribute("certicode",srand);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

// 圖象生效

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

g.dispose();

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

//設定頁面不緩存

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

response.reset();

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

response.setheader("pragma","no-cache");

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

response.setheader("cache-control","no-cache");

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

response.setdateheader("expires", 0);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

// 輸出圖象到頁面

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

servletoutputstream os=response.getoutputstream();

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

imageio.write(image, "jpeg",os);

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

os.flush();

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

os.close();

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

os=null;

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

response.flushbuffer();

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法
jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

out = pagecontext.pushbody();

jsp出現getOutputStream() has already been called for this response異常的原因和解決方法