Tomcat错误:getOutputStream() has already been called for this response
使用weblogic部署时,没有报错。客户现场使用tomcat后报错。
在tomcat下jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),
没有妥善处理好的原因。
具体的原因就是
在tomcat中jsp编译成servlet之后在函数_jspService(HttpServletRequest request, HttpServletResponse response)的最后
有一段这样的代码
finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和
response.getOutputStream()相冲突的!所以会出现以上这个异常。
然后当然是要提出解决的办法,其实挺简单的。
在使用完输出流以后调用以下两行代码即可:
out.clear();
out = pageContext.pushBody();
-----------------------------------------------------------------------------------------------------------------------------------
最后,这里是一个输出彩色验证码例子(这样的例子几乎随处可见)
imag.jsp
<%@ page import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%@ page import="java.io.OutputStream" %>
<%!
Color getRandColor(int fc,int bc){
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{
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
OutputStream os=response.getOutputStream();
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
g.setColor(getRandColor(160,200));
for (int i=0;i<155>
{
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);
}
String sRand="";
for (int i=0;i<4>
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*i+6,16);
}
session.setAttribute("rand",sRand);
g.dispose();
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();
}%>
-------------------------------------------------------------------------------------------------------------------------------
Tomcat错误:getOutputStream() has already been called for this response的更多相关文章
- Eclipse启动Tomcat错误:Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are already(转载)
转载自:http://blog.csdn.net/aigochina/article/details/7891107 Eclipse启动Tomcat错误: Several ports (8080, 8 ...
- 在Struts2中使用poi进行excel操作下载的时候报getOutputStream() has already been called for this response 错误 [转]
在项目中用到了poi这个开源的操作excel文件的jar. 项目中用到struts2容器管理servlet.不是单纯的直接用servlet. workbook.write(os); ...
- Eclipse启动Tomcat错误:Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are already
Eclipse启动Tomcat错误: Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are alread ...
- Tomcat 错误代号集
收集了一些常见的tomcat 错误代号以及附上状态代码 状态信息 含义.希望对大家有帮助. 状态代码 状态信息 含义100 Continue 初始的请求已经接受,客户应当继续发送请求的其余 ...
- Eclipse启动Tomcat错误(其他类似)
Eclipse启动Tomcat错误信息: Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are alre ...
- JSP文件下载及出现getOutputStream() has already been called for this response的解决方法
JSP文件下载及出现getOutputStream() has already been called for this response的解决方法 http://iamin.blogdriver.c ...
- getOutputStream() has already been called for this response异常的原因和解决方法
今天在调试一个小web项目时,验证码不显示了,而且后台报错 getOutputStream() has already been called for this response 经过查找得知: 在t ...
- 严重: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response
严重: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputS ...
- JSP:getOutputStream() has already been called for this response
JSP页面,用小脚本显示一张图片 <%@page import="java.io.OutputStream"%> <%@page import="jav ...
- getOutputStream() has already been called for this response解释以及解决方法
异常:getOutputStream() has already been called for this response 的解决方法 今天在第一次接触使用“验证码”功能时,在执行时出现了异常信息: ...
随机推荐
- CSS 控制鼠标在元素停留的样式
以下资料来自网络,收藏学习总结用: 有时候需要改变鼠标样式,DIV 可以改成手型等,A也可以改成光标形式 巧合要用到鼠标样式效果,就顺便整理了下十五种CSS鼠标样式,小例子供大家使用啊.CSS鼠标样式 ...
- python requests库与json数据处理详解
1. http://docs.python-requests.org/zh_CN/latest/user/quickstart.html get方法将参数放在url里面,安全性不高,但是效率高:pos ...
- PHP加密函数
单向散列加密 单向散列加密是指通过对不同输入长度的信息进行散列计算,得到固定长度的输出.这个散列计算是单向的,即不能对固定长度的输出进行计算从而获取输入信息. 特征:雪崩效应.定长输出和不可逆 作用: ...
- Linux 各种软件的安装-Apache + php 篇
mysql装好了,三剑客还差两个 apache就简单多了. yum -y install httpd # 删除默认欢迎页面 rm -f /etc/httpd/conf.d/welcome.conf s ...
- 守护进程函数——内部的小范围try catch 增强了 while死循环执行的 可靠性
void Watch() { try { LogHelper.WriteLog("WatchServi ...
- Lab 1-1
LABS The purpose of the labs is to give you an opportunity to practice the skills taught in the chap ...
- 20180429 xlVBA套打单据批量复制
Sub testCopyModelRange() Set ModelSheet = ThisWorkbook.Worksheets("单据模板") Set PrintSheet = ...
- 关于新手用java写题目,遇到的字符和字符串问题
我看到一遍很好的博客: https://blog.csdn.net/qq_37267015/article/details/78738512 1.首先了,java之中,没有像C语言那样的getchar ...
- php文件处理函数
//basename的使用$path='test/abc.jpg'; echo basename($path);// echo '<br/>'; echo basename($path,' ...
- Arthur and Brackets CodeForces - 508E (贪心,括号匹配)
大意: n个括号, 位置未知, 给出每对括号左右间距, 求输出一个合法括号序列. 最后一个括号间距一定为1, 从右往左遍历, 每次括号划分越小越好. #include <iostream> ...