getOutputStream() has already been called for this response解释以及解决方法
异常: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解释以及解决方法的更多相关文章
- Response乱码的解决方法
		
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletExcept ...
 - 在Struts2中使用poi进行excel操作下载的时候报getOutputStream() has already been called for this response 错误 [转]
		
在项目中用到了poi这个开源的操作excel文件的jar. 项目中用到struts2容器管理servlet.不是单纯的直接用servlet. workbook.write(os); ...
 - java.lang.IllegalStateException: getOutputStream() has already been called for this response
		
ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exceptionjava.lang ...
 - 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this response
		
1. 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this ...
 - 报错记录:getOutputStream() has already been called for this response
		
仅作记录:参考文章:http://www.blogjava.net/vickzhu/archive/2008/11/03/238337.html 报错信息: java.lang.IllegalStat ...
 - 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
		
http://qify.iteye.com/blog/747842 —————————————————————————————————————————————————— getOutputStream ...
 - 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 ...
 
随机推荐
- centos6.7环境之kvm虚拟化quem工具配置及使用详解
			
环境准备 需要勾选CPU的虚拟化支持,支持cpu虚拟化的CPU列表: intel支持虚拟化技术CPU列表: Intel 6 Cores / 12 Threads CPU Number: Code Na ...
 - linux设备模型:扩展篇
			
Linux设备模型组件:总线 一.定义:总线是不同IC器件之间相互通讯的通道;在计算机中,一个总线就是处理器与一个或多个不同外设之间的通讯通道;为了设备模型的目的,所有的设备都通过总线相互连接,甚至 ...
 - Expm 9_2 有向图的强连通分量问题
			
[问题描述] 给定一个有向图,设计一个算法,求解并输出该图的各个强连通分量. package org.xiu68.exp.exp9; import java.util.ArrayList; imp ...
 - 使用 HTTP/2 提升性能的几个建议
			
历史悠久的超文本传输协议,即HTTP标准,最近版本升级了.HTTP/2在2015年5月被批准,目前已经在很多Web浏览器和服务器中得到实现(包括NGINX Plus和开源NGINX).大约有三分之二的 ...
 - LeetCode(47):全排列 II
			
Medium! 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 这道 ...
 - pytest三:fixture_conftest.py 自定义测试用例的预置条件(setup)
			
用例加 setup 和 teardown 可以实现在测试用例之前或之后加入一些操作,但返种是整个脚本全局生效的,如果我想实现以下场景:用例 1 需要先登录,用例 2 不需要登录,用例 3 需要先登录. ...
 - easyUI拖动购物车案例
			
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
 - HDU 3980 (SG 环变成链 之前的先手变成后手)
			
题意 两个人在一个由 n 个玻璃珠组成的一个圆环上玩涂色游戏,游戏的规则是: 1.每人一轮,每轮选择一个长度为 m 的连续的.没有涂过色的玻璃珠串涂色 2.不能涂色的那个人输掉游戏 Aekdycoin ...
 - CentOS6.8安装360 pika
			
1.安装依赖包 yum install snappy-devel bz2 libzip-dev libsnappy-dev libprotobuf-dev libevent-dev protobuf- ...
 - c++中关于用stringstream进行的类型转化
			
1.将int转化成string类型 #include <iostream> #include <sstream> using namespace std; int main() ...