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

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

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

    cuiyi's blog(崔毅 crazycy)

    記錄點滴 鑒往事之得失 以資于發(fā)展
    數(shù)據(jù)加載中……

    論技術(shù)官方似乎確比民間友好,jstl之c:import之惑(二)

    論技術(shù)官方似乎確比民間友好,strust之bean:include之感(一)
    論技術(shù)官方似乎確比民間友好,jstl之c:import之感(二)

    上篇文章“論技術(shù)官方似乎確比民間友好,struts之bean:include之感(一)”中分析了struts的bean:include的不足;
    用c:import似乎是個比較完善的解決方案;
    然而一物終降一物,工程應(yīng)用的復(fù)雜,必然引出新的挑戰(zhàn),本文主要分析c:import的困惑


    Exception
    IllegalStateException: Cannot forward after response has been committed

    Exception情景

    1)在Servlet1中用forward轉(zhuǎn)向jsp1頁面,然后JSP1的頁面有一個鏈接,這個連接指向Servlet2,然后Servlet2再用forward轉(zhuǎn)向jsp2
    2)Action1
         mapping.find("a.jsp")

         a.jsp
         <c:import url="b.do?method=doB&id=aid">

    Reason

    遇到Cannot forward after response has been committed,是由于有頁面顯示后仍然含請求轉(zhuǎn)向產(chǎn)生的,而當頁面有輸出之后是不能夠重定向的,同理還適用于  
      repsonse.sendRedirect();  
      <jsp:forward   page=""/>

    至于<c:import>造成的IllegalStateException: Cannot forward after response has been committed原因
    是因為某些時候Action的資料量太大,超出JSP默認的8kb,就會被迫使先把response傳給使用者,這也就早晨管理其他資料沒有辦法完全傳遞過來,傳遞過來的時候響應(yīng)已經(jīng)做出了

    Reference

    Hans's Top Ten JSP TipsDealing with Buffer Flushing Issues

    An HTTP response message contains both headers and a body. The headers tell the browser things like what type of data the body contains (HTML text, an image), the size of the body, if the body can be cached, and so on. Headers are also used to set cookies and to tell the browser to automatically get another page (a redirect). All response headers must be sent to the browser before the body is sent.

    To allow parts of the body to be produced (from static template text as well as content generated dynamically by JSP elements) before headers are set, the body is buffered. Instead of sending the response to the browser as soon as something is written to the response body, the JSP container writes all static markup code and all dynamic content generated by JSP elements to the buffer. At some point, such as when the buffer is full or the end of the page is reached, the container sends all headers that have been set followed by the buffered body content. In servlet speak, this is called committing the response. After the response has been committed, you can't set headers, such as for cookies or a redirection instruction. Another thing you can't do is forward the request to another page.

    In most cases, this is not a problem. The default buffer size is 8KB, more than enough for a typical page, and you can increase it with the buffer attribute of the page directive. But if you use the include action in a page, you may be in for a surprise. Due to limitations in the way the servlet features used by <jsp:include> are specified, the buffer is always flushed before the target page is invoked. This means that you can't set headers or use <jsp:forward> after a <jsp:include> action.

    An unfortunate side-effect of this automatic flushing is that runtime errors triggered by JSP elements after a <jsp:include> action may not be reported correctly, since many JSP containers use the forward mechanism to display the error page. If you see an error message like "response already committed" in a page with <jsp:include> elements, I suggest that you use the include directive instead (at least until you have isolated the problem).

    Hans's Top Ten JSP TipsChoosing the Right Include Mechanism

    A JSP page can include page fragments from other files to form the complete response. You can use this, for instance, to keep header, footer, and navigation bar content in separate files and include them in all other pages. There are two include mechanisms: the include directive and the include action. It's not always obvious which one to use, though.

    The include directive, <%@ include file="filename.inc" %>, includes the content of the specified file during the translation phase--when the page is converted to a servlet. The main page and the included file are simply merged. This means that scripting variables declared in one file (using scripting elements or an action element like <jsp:useBean>) are visible in all files and must have unique names. Some containers detect changes in files included with the directive, but the specification doesn't require it. Therefore, changes you make to the included file in a running system may not be reflected immediately; you may have to update the main JSP page, or remove the class file generated for the main page in order to see the change.

    The include action, <jsp:include page="pagename.jsp" flush="true" />, includes the response generated by executing the specified page (a JSP page or a servlet) during the request processing phase--when the page is requested by a user. As opposed to the include directive, the page name can be specified as a so-called request-time attribute value, so which page to include can be decided when the main page is requested. Since it's the response generated by the page that is included, not the content of the page itself, scripting variables declared in one file are not available to the other files. To share an object between the pages you must instead place it in one of the following JSP scopes: request, session or application scope. If you change a page included with the include action, the change always takes effect immediately.

    My rule of thumb for when to use the different mechanisms is this:

    o    Use the include directive if the file changes rarely. It's the fastest mechanism. If your container doesn't automatically detect changes, you can force the changes to take effect by deleting the main page class file.

    o    Use the include action only for content that changes often, and if which page to include cannot be decided until the main page is requested.

    Solution

    1) 加大jsp buff

     How<%@ page buffer="16kb" %>

    Doubt: what would be if the infos size larger than specified buffer? No problem, 8k is enough for one page

    2) absolute path

    Would make context imported part independence of the caller context;就是說<c:import>引入的內(nèi)容和當前整個頁面的上下文并不完全一樣。

    3) 若為Servlet類
    解決辦法,在request.getRequestDispatcher("*.jsp").forward(request, response);后加return;OK

    4)若URL里沒有變量,用<%@ page=''%>即可




    posted on 2007-11-15 19:11 crazycy 閱讀(3771) 評論(0)  編輯  收藏 所屬分類: JavaEE技術(shù)

    主站蜘蛛池模板: 色欲aⅴ亚洲情无码AV蜜桃| 国产三级免费电影| 国内精品99亚洲免费高清| 亚洲综合一区无码精品| 成人免费区一区二区三区| 亚洲阿v天堂在线2017免费| 亚洲av无码一区二区三区四区| 永久看日本大片免费35分钟| 亚洲激情视频在线观看| 久久精品视频免费| 亚洲人成网7777777国产| 国产黄在线播放免费观看| 亚洲成?v人片天堂网无码| 美女免费精品高清毛片在线视| 免费国产黄线在线观看| 亚洲精品乱码久久久久久V | 四虎永久在线精品免费影视| 亚洲综合精品成人| 成人毛片视频免费网站观看| 精品久久久久久亚洲精品| 最近最新的免费中文字幕| 亚洲午夜理论片在线观看| 中文字幕免费在线观看| 久久精品国产亚洲香蕉| 中文字幕乱码系列免费| 精品国产_亚洲人成在线高清| 三年片免费观看大全国语| 亚洲国产精品无码久久SM| 无码国产精品一区二区免费16| 亚洲国产AV无码专区亚洲AV| 外国成人网在线观看免费视频| 亚洲一区二区电影| 18成禁人视频免费网站| 精品国产日韩久久亚洲| 免费人成网站在线观看不卡| 亚洲AV成人片色在线观看| 暖暖日本免费在线视频 | 国产裸模视频免费区无码| 成人黄网站片免费视频| 亚洲国产日产无码精品| 亚洲人成网站18禁止一区|