异常: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. nagios系列(一)centos6.5环境部署nagios服务端

    nagios软件安装包存放目录:/home/oldboy/tools nagios服务安装目录:/usr/local/nagios 1.配置yum源 echo "------ step 1: ...

  2. mongodb数据库集群及sharding分片配置

    复制集群的配置 1.安装mongodb数据库 在主节点和从节点上都安装mongodb # rpm -ivh mongo-10gen-2.4.6-mongodb_1.x86_64.rpm mongo-1 ...

  3. eol-last的相关知识

    eslint  “eol-last”:0 文件末尾强制换行(就是代码结尾处,要来个空格,相当于加一行,设置为0就可以了) ./src/main.js error  eol-last  Newline ...

  4. 如何理解深度学习中的Transposed Convolution?

    知乎上的讨论:https://www.zhihu.com/question/43609045?sort=created 不过看的云里雾里,越看越糊涂. 直到看到了这个:http://deeplearn ...

  5. tomcat 拒绝服务

    一 尝试重新下载二进制安装包安装包 wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.16/bin/apache-tomcat-9.0 ...

  6. 性能测试二十:环境部署之Tomcat多实例部署+日志监控

    一个tomcat性能有限,所以需要部署等多个tomcat 单实例部署与windows下类似,项目包放到webapp目录下,启动bin目录下的startup.sh即可启动命令:./startup.sh启 ...

  7. springMVC源码分析--FlashMap和FlashMapManager重定向数据保存

    在上一篇博客springMVC源码分析--页面跳转RedirectView(三)中我们看到了在RedirectView跳转时会将跳转之前的请求中的参数保存到fFlashMap中,然后通过FlashMa ...

  8. 点分治 poj1741

    题意: 给出一颗树,询问有多少对点对距离<=k 链接: http://poj.org/problem?id=1741 题解: 点分治的模板题 点分治即采用分治思想分而治之 考虑一颗子树内距离&l ...

  9. noip2016 天天爱跑步

    没看过正解..应该是些乱七八糟想不出来的东西 解法1: 首先,必须要做的是将每条路径拆成2个直的路径 那么对于那条从深度大的到深度小的路径 dep[x]-dep[y]应该等于观察时间 那么就可以在这些 ...

  10. Nginx 启动脚本,超级详细

    转载自:https://www.cnblogs.com/leffss/p/7845303.html