使用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的更多相关文章

  1. 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 ...

  2. 在Struts2中使用poi进行excel操作下载的时候报getOutputStream() has already been called for this response 错误 [转]

    在项目中用到了poi这个开源的操作excel文件的jar. 项目中用到struts2容器管理servlet.不是单纯的直接用servlet.         workbook.write(os);   ...

  3. 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 ...

  4. Tomcat 错误代号集

    收集了一些常见的tomcat 错误代号以及附上状态代码  状态信息  含义.希望对大家有帮助. 状态代码  状态信息  含义100  Continue  初始的请求已经接受,客户应当继续发送请求的其余 ...

  5. Eclipse启动Tomcat错误(其他类似)

    Eclipse启动Tomcat错误信息: Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are alre ...

  6. JSP文件下载及出现getOutputStream() has already been called for this response的解决方法

    JSP文件下载及出现getOutputStream() has already been called for this response的解决方法 http://iamin.blogdriver.c ...

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

    今天在调试一个小web项目时,验证码不显示了,而且后台报错 getOutputStream() has already been called for this response 经过查找得知: 在t ...

  8. 严重: 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 ...

  9. JSP:getOutputStream() has already been called for this response

    JSP页面,用小脚本显示一张图片 <%@page import="java.io.OutputStream"%> <%@page import="jav ...

  10. getOutputStream() has already been called for this response解释以及解决方法

    异常:getOutputStream() has already been called for this response 的解决方法 今天在第一次接触使用“验证码”功能时,在执行时出现了异常信息: ...

随机推荐

  1. 全排列问题(c语言实现)

    问题描述: 假设有数组里面存放26个字母,取出n个,以m个排列,计算排列的总数! 注意: (1) m<n (2) 里面的元素不能重复排列 (3)"遇零则止" 核心代码如下: ...

  2. 不要在Lua中使用os.clock()函数

    1.os.clock函数的实现是调用了c语言的函数函数库,实现代码如下: static int os_clock (lua_State *L) { lua_pushnumber(L, ((lua_Nu ...

  3. English Voice of <<Dream it passible>>

    Dream It Possible(梦想成为可能) - DelaceyI will run I will climb I will soar.我奔跑,我攀爬 我要飞翔.I'm undefeated我所 ...

  4. ASA与N6K对接

    ASA5545配置interface GigabitEthernet0/0 channel-group 10 mode active no nameif no security-level no ip ...

  5. linux文件系统(一)

    linux的文件系统以及文件类型一.linux 文件系统: 根文件系统(rootfs) rootfilesystem /etc,/usr,/var,/home,/dev 系统自我运行必须用到的路径:( ...

  6. 微信小程序选择视频,视频上传,视频播放

    请查看链接地址看具体详情: 选择视频: https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-video.html#wxchoosevideoobje ...

  7. pytorch构建自己设计的层

    下面是如何自己构建一个层,分为包含自动反向求导和手动反向求导两种方式,后面会分别构建网络,对比一下结果对不对. -------------------------------------------- ...

  8. linux搭建DHCP

    动态主机配置协议(DHCP,Dynamic Host Configuration Protocol),该协议用于自动管理局域网内主机的IP地址.子网掩码.网关地址及DNS地址等参数,可以有效地提升IP ...

  9. MySQL的自动补全和语法高亮工具MyCli

    官方地址: RHEL, Centos: We don't have packages for RHEL or Centos, yet. Instead, use pip to install mycl ...

  10. JQ 实现监测input中值的变化并绑定到另个input

                    $('#input').bind('input propertychange', function () {                     $('#myDiv ...