vickzhu

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  151 随笔 :: 0 文章 :: 34 评论 :: 0 Trackbacks
    getOutputStream() has already been called for this response 这个错误遇到过不少次,网上看到大多不能解决问题。
    下面两点是我自己总结出来的:
    1、在我们应用验证码时,都会用到字节流response.getOutputStream()来将验证码输出,但是jsp页面自己最后会调用字符流JspWriter的out()方法将页面的内容输出。通过查看servlet的API我们可以看到知道,在servlet中不能够同时利用这两个流输出,解决办法将验证码写在servlet中,具体见下面。
    2、相信请求转发( request.getRequestDispacher().forward() )和请求跳转( response.sendRedirect() )的区别大家都知道。其中request.getRequestDispacher().forward() 方法的调用者与被调用者之间共享相同的request对象和response对象,它们属于同一个访问请求和响应过程。JSP页面转译为的_servlet会最后调用releasePageContext()方法( All PageContext objects obtained via this method shall be released by invoking releasePageContext().)释放我们页面所有的实体对象,当我们的调用者有页面输出时,就会抛出这个异常。具体原因也没有弄清楚,各位大侠如果谁知道可以告知一下。其实验证码也是同一个原理,如果我们将验证码的代码写在jsp页面中,因为jsp页面会调用JspWriter的out()方法将内容输出,同时我们的图片又调用了response.getOutputStream()方法因此会抛出这个异常;如果我们将验证码写在servlet中,就不会同时使用两种输出也就不会出错。有时即使调用者页面没有输出,也会抛出这个异常,仔细看jsp转译以后的源码发现输出了换行,因此,我们最好把调用者页面的%>和<%之间换行去掉,把%>和<%直接写在一起。
    另外,如果我们实在要在jsp中用到response.getOutputStream(),比如验证码、jspSmartUpload,我们需要在最后加入如下代码:
        response.reset();
        out.clear();
        out=pageContext.pushBody();

    有不对之处还望指教!
    
posted on 2008-11-03 14:38 筱 筱 阅读(37161) 评论(19)  编辑  收藏

评论

# re: getOutputStream() has already been called for this response 错误解决 2009-04-20 11:37 gby
好!!!  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决[未登录] 2010-01-27 15:36 steven
out.clear();
out=pageContext.pushBody();

怎么设置 out 变量?  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决[未登录] 2011-08-13 10:59 啊啊
我怎么觉得没什么用啊?还是一样的报这个错误的了?  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决[未登录] 2011-08-25 10:23 K
不错了,解决,3Q  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决 2012-03-09 11:07 xuexizhe
good
  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决 2012-04-11 20:09 Aluo
问题解决,Thank you very much!!!  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决 2012-04-20 15:30 sdf
太好了 呵呵 帮助很大  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决[未登录] 2012-05-03 15:35 zxd
太棒了,谢谢你偶像,问题解决了!!!!!  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决[未登录] 2012-05-08 15:16 hhh
按照你说的, 在后面加了那两句,换了最下面的那个错误了....
-------------------------------------------------
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.awt.image.BufferedImage,javax.imageio.ImageIO" %>
<%@page import="com.shopzdw.shop.image.Pair,com.shopzdw.shop.image.ImageOut" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%
response.setContentType("image/jpeg");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
Pair<String, BufferedImage> p = ImageOut.getImage();
ImageIO.write(p.getValue(), "JPEG", response.getOutputStream());
session.setAttribute("valid", p.getKey());



response.reset();
out.clear();
out=pageContext.pushBody();
%>

----------------------------------------------------------------
2012-5-8 15:12:50 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.reset(ResponseFacade.java:310)
at org.apache.jsp.ValidateImage_jsp._jspService(ValidateImage_jsp.java:71)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决 2012-05-09 15:50 wwt
把response.reset();去掉就可以了。
@hhh
  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决[未登录] 2012-07-02 11:39 a
那如果前台是静态页面呢?请问怎么办?  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决[未登录] 2012-07-18 10:21 lee
非常感谢,我也是一样的问题。  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决 2012-07-22 11:46 linman
很好,谢谢!  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决[未登录] 2012-07-27 18:31 xx
非常感谢  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决 2012-08-09 17:59 SSDU灬丨HJ
效果明显 谢谢!  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决 2012-09-25 12:13 lmf
@hhh
去年response.reset();即可  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决 2014-10-22 16:16 jsp
我的jsp页面并没有用到response.getOutputStream();呢?但是还是报了getOutputStream() has already been called for this response错  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决 2015-04-22 14:44 lpy
3q问题解决了  回复  更多评论
  

# re: getOutputStream() has already been called for this response 错误解决[未登录] 2016-04-08 17:44 s
asda  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: