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

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

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

    Java Blog for Alex Wan

    Let life be beautiful like summer flowers and death like autumn leaves.

    統計

    留言簿(10)

    BlogJava

    Blogs

    DIV+CSS

    JQuery相關

    友情鏈接

    常去的地方

    數據供應

    閱讀排行榜

    評論排行榜

    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()相沖突的!所以會出現以上這個異常。

    然后當然是要提出解決的辦法,其實挺簡單的(并不是和某些朋友說的那樣--
    將jsp內的所有空格和回車符號所有都刪除掉),

    在使用完輸出流以后調用以下兩行代碼即可:
    out.clear();
    out = pageContext.pushBody();

    最后這里是一個輸出彩色驗證碼例子(這樣的例子幾乎隨處可見)
    imag.jsp

    <%@ page  import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
    <%@ page import="java.io.OutputStream" %>
    <%!
    Color getRandColor(int fc,int bc){
    Random random = new Random();
    if(fc>255) fc=255;
    if(bc>255) bc=255;
    int r=fc+random.nextInt(bc-fc);
    int g=fc+random.nextInt(bc-fc);
    int b=fc+random.nextInt(bc-fc);
    return new Color(r,g,b);
    }
    %>
    <%
    try{
    response.setHeader("Pragma","No-cache");
    response.setHeader("Cache-Control","no-cache");
    response.setDateHeader("Expires", 0);
    int width=60, height=20;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    OutputStream os=response.getOutputStream();
    Graphics g = image.getGraphics();
    Random random = new Random();
    g.setColor(getRandColor(200,250));
    g.fillRect(0, 0, width, height);

    g.setFont(new Font("Times New Roman",Font.PLAIN,18));
    g.setColor(getRandColor(160,200));
    for (int i=0;i<155;i++)
    {
    int x = random.nextInt(width);
    int y = random.nextInt(height);
    int xl = random.nextInt(12);
    int yl = random.nextInt(12);
    g.drawLine(x,y,x+xl,y+yl);
    }
    String sRand="";
    for (int i=0;i<4;i++){
    String rand=String.valueOf(random.nextInt(10));
    sRand+=rand;
    g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
    g.drawString(rand,13*i+6,16);
    }
    session.setAttribute("rand",sRand);
    g.dispose();

    ImageIO.write(image, "JPEG",os);
    os.flush();
    os.close();
    os=null;
    response.flushBuffer();
    out.clear();
    out = pageContext.pushBody();
    }
    catch(IllegalStateException e)
    {
    System.out.println(e.getMessage());
    e.printStackTrace();
    }%>


    如有不足之處,歡迎斧正! 



    Let life be beautiful like summer flowers and death like autumn leaves.

    posted on 2008-06-07 10:27 Alexwan 閱讀(358) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    主站蜘蛛池模板: 亚洲高清最新av网站| 久久无码av亚洲精品色午夜| 亚洲天堂在线视频| 最近中文字幕免费mv视频8| 91在线免费视频| 成a人片亚洲日本久久| 亚洲乱码一二三四区国产| 国产亚洲综合色就色| 免费又黄又爽又猛的毛片| 男男AV纯肉无码免费播放无码| 久别的草原电视剧免费观看| 国产无限免费观看黄网站| 小说专区亚洲春色校园| 亚洲日本在线电影| 亚洲国产高清美女在线观看| 亚洲精选在线观看| 日韩va亚洲va欧洲va国产| 亚洲中文字幕无码久久精品1| 国产一级淫片免费播放电影| 午夜高清免费在线观看| 可以免费看黄的网站| 精品熟女少妇av免费久久| 黄色片免费在线观看| a国产成人免费视频| fc2成年免费共享视频18| 日日狠狠久久偷偷色综合免费| 色偷偷噜噜噜亚洲男人| 亚洲午夜精品久久久久久app| 亚洲伊人久久大香线蕉影院| 亚洲精品综合久久中文字幕| 亚洲综合精品香蕉久久网97| 精品亚洲国产成AV人片传媒| 亚洲视屏在线观看| 亚洲理论在线观看| 亚洲伊人久久大香线焦| 亚洲国产成人精品无码一区二区 | 免费看黄网站在线看 | 性xxxxx免费视频播放| 免费观看国产网址你懂的| 国产曰批免费视频播放免费s| 免费H网站在线观看的|