问题:
在打开 http://localhost:8080/bertelsmann/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()相冲突的!所以会出现以上这个异常。
在打开 http://localhost:8080/bertelsmann/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()相冲突的!所以会出现以上这个异常。