vickzhu

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            151 隨筆 :: 0 文章 :: 34 評(píng)論 :: 0 Trackbacks
              getOutputStream() has already been called for this response 這個(gè)錯(cuò)誤遇到過不少次,網(wǎng)上看到大多不能解決問題。
              下面兩點(diǎn)是我自己總結(jié)出來的:
              1、在我們應(yīng)用驗(yàn)證碼時(shí),都會(huì)用到字節(jié)流response.getOutputStream()來將驗(yàn)證碼輸出,但是jsp頁(yè)面自己最后會(huì)調(diào)用字符流JspWriter的out()方法將頁(yè)面的內(nèi)容輸出。通過查看servlet的API我們可以看到知道,在servlet中不能夠同時(shí)利用這兩個(gè)流輸出,解決辦法將驗(yàn)證碼寫在servlet中,具體見下面。
              2、相信請(qǐng)求轉(zhuǎn)發(fā)( request.getRequestDispacher().forward() )和請(qǐng)求跳轉(zhuǎn)( response.sendRedirect() )的區(qū)別大家都知道。其中request.getRequestDispacher().forward() 方法的調(diào)用者與被調(diào)用者之間共享相同的request對(duì)象和response對(duì)象,它們屬于同一個(gè)訪問請(qǐng)求和響應(yīng)過程。JSP頁(yè)面轉(zhuǎn)譯為的_servlet會(huì)最后調(diào)用releasePageContext()方法( All PageContext objects obtained via this method shall be released by invoking releasePageContext().)釋放我們頁(yè)面所有的實(shí)體對(duì)象,當(dāng)我們的調(diào)用者有頁(yè)面輸出時(shí),就會(huì)拋出這個(gè)異常。具體原因也沒有弄清楚,各位大俠如果誰(shuí)知道可以告知一下。其實(shí)驗(yàn)證碼也是同一個(gè)原理,如果我們將驗(yàn)證碼的代碼寫在jsp頁(yè)面中,因?yàn)閖sp頁(yè)面會(huì)調(diào)用JspWriter的out()方法將內(nèi)容輸出,同時(shí)我們的圖片又調(diào)用了response.getOutputStream()方法因此會(huì)拋出這個(gè)異常;如果我們將驗(yàn)證碼寫在servlet中,就不會(huì)同時(shí)使用兩種輸出也就不會(huì)出錯(cuò)。有時(shí)即使調(diào)用者頁(yè)面沒有輸出,也會(huì)拋出這個(gè)異常,仔細(xì)看jsp轉(zhuǎn)譯以后的源碼發(fā)現(xiàn)輸出了換行,因此,我們最好把調(diào)用者頁(yè)面的%>和<%之間換行去掉,把%>和<%直接寫在一起。
              另外,如果我們實(shí)在要在jsp中用到response.getOutputStream(),比如驗(yàn)證碼、jspSmartUpload,我們需要在最后加入如下代碼:
                  response.reset();
                  out.clear();
                  out=pageContext.pushBody();

              有不對(duì)之處還望指教!
              
          posted on 2008-11-03 14:38 筱 筱 閱讀(37279) 評(píng)論(19)  編輯  收藏

          評(píng)論

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決 2009-04-20 11:37 gby
          好!!!  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決[未登錄] 2010-01-27 15:36 steven
          out.clear();
          out=pageContext.pushBody();

          怎么設(shè)置 out 變量?  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決[未登錄] 2011-08-13 10:59 啊啊
          我怎么覺得沒什么用啊?還是一樣的報(bào)這個(gè)錯(cuò)誤的了?  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決[未登錄] 2011-08-25 10:23 K
          不錯(cuò)了,解決,3Q  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決 2012-03-09 11:07 xuexizhe
          good
            回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決 2012-04-11 20:09 Aluo
          問題解決,Thank you very much!!!  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決 2012-04-20 15:30 sdf
          太好了 呵呵 幫助很大  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決[未登錄] 2012-05-03 15:35 zxd
          太棒了,謝謝你偶像,問題解決了!!!!!  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決[未登錄] 2012-05-08 15:16 hhh
          按照你說的, 在后面加了那兩句,換了最下面的那個(gè)錯(cuò)誤了....
          -------------------------------------------------
          <%@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
          嚴(yán)重: 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)
            回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決 2012-05-09 15:50 wwt
          把response.reset();去掉就可以了。
          @hhh
            回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決[未登錄] 2012-07-02 11:39 a
          那如果前臺(tái)是靜態(tài)頁(yè)面呢?請(qǐng)問怎么辦?  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決[未登錄] 2012-07-18 10:21 lee
          非常感謝,我也是一樣的問題。  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決 2012-07-22 11:46 linman
          很好,謝謝!  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決[未登錄] 2012-07-27 18:31 xx
          非常感謝  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決 2012-08-09 17:59 SSDU灬丨HJ
          效果明顯 謝謝!  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決 2012-09-25 12:13 lmf
          @hhh
          去年response.reset();即可  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決 2014-10-22 16:16 jsp
          我的jsp頁(yè)面并沒有用到response.getOutputStream();呢?但是還是報(bào)了getOutputStream() has already been called for this response錯(cuò)  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決 2015-04-22 14:44 lpy
          3q問題解決了  回復(fù)  更多評(píng)論
            

          # re: getOutputStream() has already been called for this response 錯(cuò)誤解決[未登錄] 2016-04-08 17:44 s
          asda  回復(fù)  更多評(píng)論
            


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 柯坪县| 桂林市| 广灵县| 洱源县| 延寿县| 辉南县| 博野县| 仙游县| 呼图壁县| 夏津县| 上蔡县| 东光县| 和龙市| 无为县| 库伦旗| 平邑县| 珠海市| 永兴县| 六安市| 镇宁| 施甸县| 肇东市| 高清| 通州区| 平塘县| 福安市| 佛冈县| 柏乡县| 咸丰县| 东台市| 龙州县| 尚义县| 滨海县| 文登市| 兴仁县| 巨鹿县| 西盟| 平定县| 婺源县| 蒙阴县| 江津市|