解决getOutputStream() has already been called for this response异常办法

在打开http://localhost:8080/firsttest/login.jsp
时系统报getOutputStream() has already been called for this response 这个异常.因为登陆的jsp页
面用到了输出流response.getWriter(),所以在网上搜索了一下.原因是登陆页面的验证码输入框和jsp页的
response的response.getOutputStream().生产冲突,所以会报那个异常.解决方法
原本的验证图片输出码:
<%OutputStream o = response.getOutputStream();%>
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.awt.Color, java.awt.Font, java.awt.FontMetrics, java.awt.Graphics, com.sun.image.codec.jpeg.JPEGCodec,
     java.awt.image.BufferedImage, java.io.File, java.io.OutputStream, java.io.IOException,
     com.sun.image.codec.jpeg.JPEGImageEncoder,java.util.Random"%>
<%!static Random ran = new Random(System.currentTimeMillis());%>
<%!static final int width = 50, height = 16;%>
<%
 int word = (ran.nextInt(9) + 1) * 1000 + ran.nextInt(1000);
 if(session.getAttribute("checkout")!=null)
 session.removeAttribute("checkout");
 session.setAttribute("checkout", word);
 BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
 Graphics graphics = img.getGraphics();
 graphics.setColor(Color.WHITE);
 graphics.fillRect(0, 0, width, height);//背景是白色的
  
 //设置字体
 graphics.setColor(Color.BLUE);
 Font font = new Font("宋体", Font.ITALIC + Font.BOLD, height - 1);
 graphics.setFont(font);
 FontMetrics fontMetrics = graphics.getFontMetrics();
 int word_width = fontMetrics.stringWidth("" + word);//字符的长度
 graphics.drawString("" + word, (width - word_width) / 2 - 1, height / 2 + 5);
 JPEGImageEncoder encoder = null;
 try {
  encoder = JPEGCodec.createJPEGEncoder(o);
  encoder.encode(img);
  o.flush();
  /**
   * 这两段为修改后加上去的.
  **/ 
  out.clear();
  out=pageContext.pushBody();
 }  finally {
  
 }

%>

网上的解释:
在tomcat中jsp编译成servlet之后在函数_jspService(HttpServletRequest request, HttpServletResponse response)的最后
有一段这样的代码
finally {
      if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
    }
这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和
response.getOutputStream()相冲突的!所以会出现以上这个 异常 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值