<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熱點博客

    好友博客

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲av日韩综合一区在线观看| 污污网站18禁在线永久免费观看| 亚洲国产成人久久一区WWW| 人妻仑刮八A级毛片免费看| 国产亚洲欧洲Aⅴ综合一区| 一级毛片在播放免费| 久久乐国产综合亚洲精品| 狠狠色婷婷狠狠狠亚洲综合| 最近在线2018视频免费观看| 你是我的城池营垒免费看| 日本亚洲中午字幕乱码| 亚洲精品国产高清在线观看| 亚洲乱码日产精品BD在线观看| 久久亚洲AV午夜福利精品一区| 亚洲精品tv久久久久| 免费国产综合视频在线看| 色窝窝免费一区二区三区| 99久久久精品免费观看国产| 永久免费av无码网站yy| 久久国产精品成人免费| 国产拍拍拍无码视频免费| 久久免费国产视频| 在线免费观看国产| 毛片免费在线观看网站| 成人无码区免费A片视频WWW| 一个人在线观看视频免费| 成人午夜大片免费7777| 国产精品免费视频一区| 一区二区三区亚洲视频| AV在线播放日韩亚洲欧| 亚洲综合熟女久久久30p| 亚洲成熟xxxxx电影| 亚洲中文字幕无码爆乳app| 色天使亚洲综合一区二区| 边摸边吃奶边做爽免费视频网站| 一本岛v免费不卡一二三区| 久久免费视频一区| 57pao国产成永久免费视频 | 国产99视频精品免费专区| 2015日韩永久免费视频播放 | 亚洲六月丁香六月婷婷色伊人 |