`
zys08
  • 浏览: 142728 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

getOutputStream() has already been called for this response 的解决方法

 
阅读更多

今天在jsp页面中写java代码进行文件下载的时候出现如下问题:

严重: 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()相冲突的!所以会出现以上这个异常。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics