异常: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. Bootstrap报错:Bootstrap's JavaScript requires jQuery

    如题,经百度原来导入顺序的问题,须要先导入Jqeury库,今记之!

  2. adb devices检测不到夜神模拟器

    1.dos下,cd进入到夜神模拟器的bin目录 代码: nox_adb connect 127.0.0.1:62001 2.dos下,进入进Android SDK下的platform-tools目录 ...

  3. oracle进阶之connect by笔记

    本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处. http://www.cnblogs.com/king-xg/p/6794562.html 如果觉得对您有帮 ...

  4. Best quotes from The Vampire Diary(《吸血鬼日记》经典台词)

    1. I will start fresh, be someone new. 1. 我要重新开始,做不一样的自己. 2. It's the only way I'll make it through. ...

  5. poj1195二维树状数组模板

    二维树状数组和一维的也差不多,改一下add和query函数即可:即按行修改,行内单点修改即可 /* 二维树状数组,询问一个二维区间内的数之和 */ #include<iostream> # ...

  6. python 全栈开发,Day30(纸牌游戏,异常和错误,异常处理)

    一.纸牌游戏                                                                                              ...

  7. StackOverflowError的原因

    package chapter04; /** 如果两个方法出现互相调用的时候会出现StackOverflowError*/ public class C06_Method { public stati ...

  8. List遍历三种方法:1.for 2.增强性for 3.迭代器

    package chapter09; import java.util.ArrayList;import java.util.Iterator;import java.util.List; /* * ...

  9. javascript如何从tr中分别获得每个td的元素

    <html> <body> <table border="1" id="table1"> <tr id="r ...

  10. JVM启动过程 类加载器

    下图来自:http://blog.csdn.net/jiangwei0910410003/article/details/17733153 package com.test.jvm.common; i ...