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

  今天在第一次接触使用“验证码”功能时,在执行时出现了异常信息:

  严重: Servlet.service() for servlet jsp threw exception
  java.lang.IllegalStateException: getOutputStream() has already been called for this response

  。。。。

  在网上搜索之后的解决方法是:

  在生成验证码的jsp文件末尾添加两句话

  out.clear();
out = pageContext.pushBody();

  ===========================================================================

  查找的原文如下:

  ()

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

  在tomcat5下jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),
没有妥善处理好的原因。
具体的原因就是
在tomcat中jsp编译成servlet之后在函数_jspService(HttpServletRequest request, HttpServletResponse response)的最后
有一段这样的代码
finally {
   if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
  }
这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和
response.getOutputStream()相冲突的!所以会出现以上这个异常。

  采用方法很简单.在使用OutputStream输出流完成后,调用下面2个方法即可解决该问题:
out.clear();
out = pageContext.pushBody();

  示例代码:

  OutputStream os=response.getOutputStream();
os.write(new String("true  "+"nowNum=" + nowNum+"===").getBytes());
os.flush();
os.close();

  out.clear();
out = pageContext.pushBody();

  ----------------------------------------------------------

  在jsp向页面输出图片的时候,使用response.getOutputStream()会有这样的提示:java.lang.IllegalStateException:getOutputStream() has already been called for this response,会抛出Exception

  原因一:
JSP默认的输出流为PrintWriter ,即<% %>以外的东西所默认的输出方式,如果你尝试在JSP中使用ServletOutputStream就会引起错误.要嘛直接改用Servlet输出(复写service方法),要嘛删除除%$amp;>amp;$lt;%中的任何东西(包括HTML标签,空格,回车等东西)应该就可以。
对于这样的情况应该这样来解决,删除%$amp;>amp;$lt;%之间的所有内容包括空格和换行符,最后也要消除空格和换行符,最好再加上一句response.reset()。
原因二:
   
在J2EE的API参考里有这么个:

  ServletResponse的getWriter()方法里会抛出这个异常,

  IllegalStateException - if the getOutputStream method has already been called 
for this response object

  而它的getOutputStream()方法里会抛出这个异常.

  IllegalStateException - if the getOutputStream method has already been called for this response object

  并且两者的函数申明里都有这么样的一句
Either this method or getOutputStream() may be called to write the body, not both.
Either this method or getWriter() may be called to write the body, not both.
以上说明也解释了为什么在往页面中写入图片的时候要使用如下循环格式
OutputStream output=response.getOutputStream();
while((len=in.read(b)) >0) 
  {
  output.write(b,0,len);

  }
output.flush();
而不是把response.getOutputStream().write()放到循环体内

getOutputStream() has already been called for this response解释以及解决方法的更多相关文章

  1. Response乱码的解决方法

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletExcept ...

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

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

  3. java.lang.IllegalStateException: getOutputStream() has already been called for this response

    ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exceptionjava.lang ...

  4. 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this response

    1. 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this ...

  5. 报错记录:getOutputStream() has already been called for this response

    仅作记录:参考文章:http://www.blogjava.net/vickzhu/archive/2008/11/03/238337.html 报错信息: java.lang.IllegalStat ...

  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

    http://qify.iteye.com/blog/747842 —————————————————————————————————————————————————— getOutputStream ...

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

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

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

随机推荐

  1. S5PV210的根文件系统制作

    一.移植BusyBox1.下载BusyBox的源代码下载地址:http://www.busybox.net/downloads/,此处下载busybox-1.20.2.tar.bz2.2.解压并进入目 ...

  2. Ex 6_9 某个字符串处理语言提供了一个将字符串一分为二的基本操作..._第六次作业

    设字符串的长度为n,整型数组arr[0. . .n-1]的第一个数和最后一个数为开始点与结束点的位置,中间的数为拆分点的位置,设cost[i,j]为第i个分割点到第j个分割点的最小代价,两个分割点之间 ...

  3. Go语言规格说明书 之 结构体类型(Struct types)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...

  4. Vue.js+Koa2移动电商实战 笔记

    地址:http://jspang.com/ https://github.com/shenghy/SmileVue 1.vant  https://www.youzanyun.com/zanui/va ...

  5. C <string.h>常用函数介绍

    1. strcpychar *strcpy(char *destin, char *source);功能:将source指向的字符串拷到destin. int main() { ]; "; ...

  6. SpringBoot入门小案例

    1.创建一个简单的maven project项目 2.下面来看一下项目结构: 3.pom.xml 配置jar包 <parent> <groupId>org.springfram ...

  7. LeetCode(43):字符串相乘

    Medium! 题目描述: 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = &quo ...

  8. hdu5646数学构造+二分

    /* 满足n>=(k+1)*k/2的整数n必定满足 a+(a+1)+...+(a+k-1)<=n<=(a+1)+(a+2)+...+(a+k) 只要在[a,a+k]中减掉一个数字ai ...

  9. 性能测试二十五:redis-cli 命令总结

    常用命令dbsize:查看redis中的kv数量 keys *:查看redis中所有的keyset key_1 v_1:新增一个key_1,包含v_1get key_1:查看key_1中的内容del ...

  10. python+selenium+Jenkins构建自动化测试

    环境准备 本次使用JDK:1.8.tomcat:8.5.Jenkins:2.127 安装jdk 官方下载地址 安装教程参考 安装tomcat 和jenkins tomcat官方下载地址 jenkins ...