<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 262,  comments - 221,  trackbacks - 0
    《輕量級J2EE企業應用實戰》一書的第2章有一個使用SerlvetResponse輸出圖像的例子,代碼如下:

    <%
      BufferedImage image 
    = new BufferedImage(400400, BufferedImage.TYPE_INT_RGB);
      Graphics g 
    =
     image.getGraphics();
      g.fillRect(
    0,0,400,400
    );
      g.setColor(
    new Color(255,0,0
    ));  
      g.fillArc(
    20,20,100,100,30,120
    );
      g.setColor(
    new Color(0,255,0
    ));
      g.fillArc(
    20,20,100,100,150,20
    );
      g.setColor(
    new Color(0,0,255
    ));
      g.fillArc(
    20,20,100,100,270,120
    );
      g.setColor(
    new Color(0,0,0
    ));
      g.drawString(
    "red:climb" , 30080
    );
      g.drawString(
    "green:swim"300120
    );
      g.drawString(
    "blue:jump"300160
    );
      ImageIO.write(image, 
    "bmp"
    , response.getOutputStream());
      
    //
    out.clear();
      
    //out = pageContext.pushBody();

    %>

    在Tomcat下運行時拋出如下異常:

        at org.apache.catalina.connector.Response.getWriter(Response.java:601)
        at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)
        at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:
    125
    )
        at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:
    118
    )
        at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:
    185
    )
        at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:
    116
    )
        at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:
    76
    )
        
    at org.apache.jsp.pages.drawImage_jsp._jspService(drawImage_jsp.java:84)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:
    803
    )
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
    328
    )
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
    315
    )
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:
    265
    )
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:
    803
    )
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
    269
    )
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:
    188
    )
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:
    210
    )
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:
    174
    )
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:
    127
    )
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:
    117
    )
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:
    108
    )
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
    151
    )
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:
    870
    )
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:
    665
    )
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:
    528
    )
        at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:
    81
    )
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:
    685
    )
        at java.lang.Thread.run(Thread.java:
    595)

    查看轉換后的JSP代碼,發現第84行如下(綠色高亮處):

      public void _jspService(HttpServletRequest request, HttpServletResponse response)
            
    throws java.io.IOException, ServletException 
    {

        JspFactory _jspxFactory 
    = null
    ;
        PageContext pageContext 
    = null
    ;
        HttpSession session 
    = null
    ;
        ServletContext application 
    = null
    ;
        ServletConfig config 
    = null
    ;
        JspWriter out 
    = null
    ;
        Object page 
    = this
    ;
        JspWriter _jspx_out 
    = null
    ;
        PageContext _jspx_page_context 
    = null
    ;


        
    try 
    {
          _jspxFactory 
    =
     JspFactory.getDefaultFactory();
          response.setContentType(
    "text/html; charset=UTF-8"
    );
          pageContext 
    = _jspxFactory.getPageContext(this
    , request, response,
                      
    nulltrue8192true
    );
          _jspx_page_context 
    =
     pageContext;
          application 
    =
     pageContext.getServletContext();
          config 
    =
     pageContext.getServletConfig();
          session 
    =
     pageContext.getSession();
          out 
    =
     pageContext.getOut();
          _jspx_out 
    =
     out;

          out.write(
    "\r\n"
    );
          out.write(
    "\n"
    );
          out.write(
    "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");

          out.write("<html>\n");
          out.write(
    "<head>\n"
    );
          out.write(
    "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
    );
          out.write(
    "<title>Draw Image</title>\n"
    );
          out.write(
    "</head>\n"
    );
          out.write(
    "<body>\n"
    );
          out.write(
    "\r\n"
    );

      BufferedImage image 
    = new BufferedImage(400400
    , BufferedImage.TYPE_INT_RGB);
      Graphics g 
    =
     image.getGraphics();
      g.fillRect(
    0,0,400,400
    );
      g.setColor(
    new Color(255,0,0
    ));  
      g.fillArc(
    20,20,100,100,30,120
    );
      g.setColor(
    new Color(0,255,0
    ));
      g.fillArc(
    20,20,100,100,150,20
    );
      g.setColor(
    new Color(0,0,255
    ));
      g.fillArc(
    20,20,100,100,270,120
    );
      g.setColor(
    new Color(0,0,0
    ));
      g.drawString(
    "red:climb" , 30080
    );
      g.drawString(
    "green:swim"300120
    );
      g.drawString(
    "blue:jump"300160
    );
      ImageIO.write(image, 
    "bmp"
    , response.getOutputStream());
      
    //
    out.clear();
      
    //out = pageContext.pushBody();


          out.write(
    "\r\n");
          out.write(
    "</body>\n"
    );
          out.write(
    "</html>"
    );
        }
     catch (Throwable t) {
          
    if (!(t instanceof SkipPageException))
    {
            out 
    =
     _jspx_out;
            
    if (out != null && out.getBufferSize() != 0
    )
              out.clearBuffer();
            
    if (_jspx_page_context != null
    ) _jspx_page_context.handlePageException(t);
          }

        }
     finally {
          
    if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
        }

      }

    我們看到在JSP頁面釋放資源的時候,調用了ServetResponse.getWriter()方法,之后程序即拋出異常了,查看Servlet的API發現問題: 
    public java.io.PrintWriter getWriter()
    throws java.io.IOException
    Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response's character encoding has not been specified as described in getCharacterEncoding (i.e., the method just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1.

    Calling flush() on the PrintWriter commits the response.

    Either this method or getOutputStream() may be called to write the body, not both.

     

    Returns:
    a PrintWriter object that can return character data to the client
    Throws:
    UnsupportedEncodingException - if the character encoding returned by getCharacterEncoding cannot be used
    java.lang.IllegalStateException - if the getOutputStream method has already been called for this response object
    java.io.IOException - if an input or output exception occurred
    See Also:
    getOutputStream(), setCharacterEncoding(java.lang.String)
    如API所言,由于ServletResponse.getOutputStream()方法和該方法都有可能被調用,來輸出JSP頁面的內容,如果其中的一個方法被調用了,再調用另一個方法就會拋出異常。

    解決方法如下:

    將JSP頁面的最后兩行代碼的注釋去掉,這兩行代碼的作用如下:

    out.clear():清空緩存的內容。

    pageContext.pushBody():參考API

    public BodyContent pushBody()
    Return a new BodyContent object, save the current "out" JspWriter, and update the value of the "out" attribute in the page scope attribute namespace of the PageContext.

     

    Returns:
    the new BodyContent

    ·返回一個新的BodyContent(代表一個HTML頁面的BODY部分內容)
    ·保存JspWriter實例的對象out
    ·更新PageContext的out屬性的內容



    -------------------------------------------------------------
    生活就像打牌,不是要抓一手好牌,而是要盡力打好一手爛牌。
    posted on 2008-01-19 22:24 Paul Lin 閱讀(43349) 評論(10)  編輯  收藏 所屬分類: J2EE基礎


    FeedBack:
    # re: getOutputStream() has already been called for this response的解決方法
    2008-08-19 20:02 | wasw100
    不錯,對jsp有了更深的理解了,收藏了  回復  更多評論
      
    # re: getOutputStream() has already been called for this response的解決方法
    2008-12-23 11:36 | 常青
    太感謝了。這個問題郁悶了我們幾個月,后來還是改了設計給繞過去的。現在可以按照正常的設計修改代碼了。再次謝過了。  回復  更多評論
      
    # re: getOutputStream() has already been called for this response的解決方法
    2009-05-06 10:06 | dongzgguang
    非常感謝  回復  更多評論
      
    # re: getOutputStream() has already been called for this response的解決方法[未登錄]
    2010-05-17 09:09 | Jordan
    謝謝啊!
    問題解釋的很清楚, 解決方案也正確!
    希望以后多多交流, 我QQ914132900  回復  更多評論
      
    # re: getOutputStream() has already been called for this response的解決方法
    2010-07-19 11:32 | GAME OVER
    想問下博主大哥,小弟也遇到了這個情況,但是在后面加了那兩句也不行!
    后來我也去看了下提示錯誤生成的servlet文件,但是有一點不同的是我的只有這
    _jspxFactory.releasePageContext(_jspx_page_context);
    沒有前面的if語句,直接在生成servlet的文件中加if語句,但是刷新頁面還是會有異常的。實在不懂是怎么回事,只好想請教下大哥!
    小弟Email:ice-_-wind@163.com
    勞煩大哥看到了回復下!
      回復  更多評論
      
    # re: getOutputStream() has already been called for this response的解決方法[未登錄]
    2010-11-27 17:37 | test
    錯誤解決不了,我照你的做,還是一樣!!  回復  更多評論
      
    # re: getOutputStream() has already been called for this response的解決方法
    2011-04-06 20:21 | 沈林楠
    我內牛滿面啊,郁悶我一天的問題終于解決了,愛死你了  回復  更多評論
      
    # re: getOutputStream() has already been called for this response的解決方法
    2011-10-07 10:02 | 三等功
    我這樣用了還是不行啊
      回復  更多評論
      
    # re: getOutputStream() has already been called for this response的解決方法
    2013-09-17 13:31 | 冰凝火龍吟
    謝謝指點。。。。。。  回復  更多評論
      
    # re: getOutputStream() has already been called for this response的解決方法
    2014-01-07 14:52 | Hucc
    <2008年1月>
    303112345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    留言簿(21)

    隨筆分類

    隨筆檔案

    BlogJava熱點博客

    好友博客

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久夜色精品国产亚洲| 亚洲国产精品日韩| 4480yy私人影院亚洲| 特级做A爰片毛片免费看无码| 中文字幕亚洲日本岛国片| 美女视频黄.免费网址| 亚洲成AⅤ人影院在线观看| yellow视频免费看| 在线亚洲精品自拍| 免费看搞黄视频网站| 亚洲一二成人精品区| 国产精品成人观看视频免费| 精品久久亚洲中文无码| 午夜免费福利影院| 尤物视频在线免费观看| 亚洲精品夜夜夜妓女网| 91av视频免费在线观看| 亚洲色少妇熟女11p| 国产成人精品免费视频大全五级| 日韩免费在线中文字幕| 亚洲人成网77777亚洲色| 久久A级毛片免费观看| 亚洲精品人成网在线播放影院| 日本一线a视频免费观看| 国产日韩久久免费影院| 久久丫精品国产亚洲av不卡| 久久不见久久见免费影院| 国产亚洲日韩在线a不卡| 亚洲精品制服丝袜四区| 久久久久国产精品免费免费搜索| 日韩大片免费观看视频播放| 久久精品国产亚洲av麻| 在线免费观看毛片网站| 一区二区在线免费视频| 亚洲成aⅴ人片在线观| 免费一级毛片在级播放| 99视频免费播放| 国产成人亚洲综合在线| 亚洲综合一区二区精品导航| 亚洲成av人片不卡无码久久| 91免费国产在线观看|