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 ...
随机推荐
- Nodejs 实现ESL内联FreeSWITCH设定说明
一.背景说明: SIP Server IP (Centos):192.168.11.61 ,服务器IP(Windows):192.168.11.19 二.目的: 能够从192.168.11.19上通 ...
- iOS学习笔记之异步图片下载
写在前面 在iOS开发中,无论是在UITableView还是在UICollectionView中,通过网络获取图片设置到cell上是较为常见的需求.尽管有很多现存的第三方库可以将下载和缓存功能都封装好 ...
- python 全栈开发,Day18(对象之间的交互,类命名空间与对象,实例的命名空间,类的组合用法)
一.对象之间的交互 现在我们已经有一个人类了,通过给人类一些具体的属性我们就可以拿到一个实实在在的人.现在我们要再创建一个狗类,狗就不能打人了,只能咬人,所以我们给狗一个bite方法.有了狗类,我们还 ...
- Python中List的append引用赋值问题处理
Python中的对象之间赋值时是按引用传递的,如果需要拷贝对象,需要使用标准库中的copy模块. 1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象. 2. copy.deep ...
- 060 关于Hive的调优(本身,sql,mapreduce)
1.关于hive的优化 ->大表拆分小表 ->过滤字段 ->按字段分类存放 ->外部表与分区表 ->外部表:删除时只删除元数据信息,不删除数据文件 多人使用多个外部表操作 ...
- moodle 笔记
默认版块的设置: //这些变量为新课程定义了DEFAULT块变量如果设置了这个变量,它将覆盖所有其他变量,并且是唯一使用的变量s $CFG->defaultblocks_override = ' ...
- cookie和session、
https://my.oschina.net/yoyo1987/blog/156117 Session会在浏览器关闭后消失吗? 通常情况下,当我们关闭浏览器再重新打开后,我们就需要再次进行登陆(如果没 ...
- P1356 数列的整数性
P1356 数列的整数性打的骗分,在多组数据的情况下还能骗到分,可以了.又TMD是dp.f[i][j]表示+-第i个数能否达到%p后的余数j,如果f[n][0]==true就可以. #include& ...
- 基于 Jenkins+Docker+Git 的CI流程初探
在如今的互联网时代,随着软件开发复杂度的不断提高,软件开发和发布管理也越来越重要.目前已经形成一套标准的流程,最重要的组成部分就是持续集成(Continuous Integration,CI)及持续部 ...
- idea springboot热部署无效问题
Intellij IDEA 使用Spring-boot-devTools无效解决办法 springboot项目中遇到的bug <dependencies> <!--spring bo ...