getOutputStream() has already been called for this response异常的原因和解决方法

getOutputStream() has already been called for this response异常的原因和解决方法
 

tomcat5jsp出现getOutputStream() has already been called for this response异常的原因和解决方法在tomcat5jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),没有妥善处理好的原因。具体的原因就是在tomcatjsp编译成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();

 

附:产生验证码图片的文件image.jsp

 1 <% @ page contentType = " image/jpeg "
 2      import = " java.awt.*,java.awt.image.*,java.util.*,javax.imageio.* "
 3     pageEncoding = " GBK " %>
 4 <%! Color getRandColor( int  fc,  int  bc)  {//给定范围获得随机颜色
 5        Random random = new Random();
 6        if (fc > 255)
 7            fc = 255;
 8        if (bc > 255)
 9            bc = 255;
10        int r = fc + random.nextInt(bc - fc);
11        int g = fc + random.nextInt(bc - fc);
12        int b = fc + random.nextInt(bc - fc);
13        return new Color(r, g, b);
14    }
%>
15 <%
16      // 设置页面不缓存
17     response.setHeader( " Pragma " " No-cache " );
18     response.setHeader( " Cache-Control " " no-cache " );
19     response.setDateHeader( " Expires " 0 );
20      //  在内存中创建图象
21      int  width  =   60 , height  =   20 ;
22     BufferedImage image  =   new  BufferedImage(width, height,
23             BufferedImage.TYPE_INT_RGB);
24      //  获取图形上下文
25     Graphics g  =  image.getGraphics();
26      // 生成随机类
27     Random random  =   new  Random();
28      //  设定背景色
29     g.setColor(getRandColor( 200 250 ));
30     g.fillRect( 0 0 , width, height);
31      // 设定字体
32     g.setFont( new  Font( " Times New Roman " , Font.PLAIN,  18 ));
33      // 画边框
34      // g.setColor(new Color());
35      // g.drawRect(0,0,width-1,height-1);
36      //  随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
37     g.setColor(getRandColor( 160 200 ));
38      for  ( int  i  =   0 ; i  <   155 ; i ++ {
39        int x = random.nextInt(width);
40        int y = random.nextInt(height);
41        int xl = random.nextInt(12);
42        int yl = random.nextInt(12);
43        g.drawLine(x, y, x + xl, y + yl);
44    }

45      //  取随机产生的认证码(4位数字)
46     String sRand  =   "" ;
47      for  ( int  i  =   0 ; i  <   4 ; i ++ {
48        String rand = String.valueOf(random.nextInt(10));
49        sRand += rand;
50        // 将认证码显示到图象中
51        g.setColor(new Color(20 + random.nextInt(110), 20 + random
52        .nextInt(110), 20 + random.nextInt(110)));
53        //调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
54        g.drawString(rand, 13 * i + 616);
55    }

56      //  将认证码存入SESSION
57     session.setAttribute( " rand " , sRand);
58      //  图象生效
59     g.dispose();
60      //  输出图象到页面
61     ImageIO.write(image,  " JPEG " , response.getOutputStream());
62     out.clear();
63     out  =  pageContext.pushBody();
64 %>

html中使用验证码图片:

1 < img  src ="image.jsp"  id ="src"  height ="18"  alt ="看不清楚?请点击刷新"  onclick ="this.src=this.src+'?'+Math.random();"   />

你可能感兴趣的:(getOutputStream() has already been called for this response异常的原因和解决方法)