异常: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. Ext需要的文件目录

    使用ext版本信息:ext-4.1.1a <!-- 下面是引入文件需要导入的文件信息 ext-all.css ext-all.js --><link rel="styles ...

  2. 使用Let's Encrypted HPPTS你的网站

    1.前言 最近,有同事咨询我,怎么样使用Let's Encrypted部署数字证书,于是,结合自己之前的实践,简单总结下. 2.HTTPS的优势 什么加密,防篡改,防广告植入什么的,这个就不多说了.这 ...

  3. 使用 IIS 在 Windows 上托管 ASP.NET Core

    参考微软文档: https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/iis/index?tabs=aspnetcore2x 将as ...

  4. JS的浅拷贝与深拷贝

    浅拷贝 //这样的拷贝有一个问题.那就是,如果父对象的属性等于数组或另一个对象,//那么实际上,子对象获得的只是一个内存地址,而不是真正拷贝,因此存在父对象被篡改的可能. function exten ...

  5. oracle中REGEXP_SUBSTR方法的使用

    近期在做商旅机票平台,遇到这样一个问题: 有一张tt_ticket表,用来存机票信息.里边有一个字段叫schedule,表示的是行程,存储格式为:北京/虹桥 由于公司位于上海.而上海眼下有两个机场:浦 ...

  6. notepad++ 快捷键大全、notepad常用快捷键

    Notepad++ 快捷键 大全, notepad++也情有独钟,最近发现了一个快捷键,就是选中单词,ctrl+shift+enter.不过现在想知道一个快捷键,假设有三行代码,选中后一般按TAB就可 ...

  7. linux 后台运行nohup & ctrl+z

    使用Linux时,经常希望有些命令结果不在前台显示,如sh脚本,耗时的命令等.一般情况下,使用 & 将命令结果后台运行,如sh test.sh,脚本后台执行. 有时候命令已经在前台执行了,需要 ...

  8. TestNG测试方法

    @Test(enabled = false)有助于禁用此测试用例. 分组测试是TestNG中的一个新的创新功能,使用<groups>标记在testng.xml文件中指定分组. 它可以在&l ...

  9. 性能测试五:jmeter进阶之后置处理器(正则、json提取器)

    如,从get返回的json中提取stock的值 作为post的请求参数 1.JSON提取器 专门对json数据进行提取的后置处理器 Debug Sampler:记录之前的请求的所有参数及数据 2.正则 ...

  10. MySQL重复数据处理

    最近工作很忙,很久没写博客,嘻嘻!今天写一遍关于MySQL重复数据处理的博客!前不久有个开发问我,能不能把重复的数据去除,留下唯一的数据.然后我问他为什么有这么重复的数据呢,他说写了程序去爬数据,爬到 ...