JSP技術分享:常用禁止緩衝的四種方法

來源:互聯網
上載者:User

【IT168 技術】以下是常用禁止緩衝的四種方法:

  1、用戶端緩衝要在head中加入類似如下內容:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">

  或
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

  2、在伺服器的動態網頁中禁止緩衝,要加入類似如下指令碼:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);

  3、設定有限時間的緩衝:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->int minutes = 10;
Date d = new Date();
String modDate = d.toGMTString();
String expDate = null;
expDate = (new Date(d.getTime() + minutes * 60000)).toGMTString();
response.setHeader("Last-Modified", modDate);
response.setHeader("Expires", expDate);
response.setHeader("Cache-Control", "public"); //   HTTP/1.1
response.setHeader("Pragma", "Pragma"); //   HTTP/1.0

  建議:jsp cache最好做在過濾器上,把需要緩衝的頁面集中在同一個目錄下,每次更改只須更改web.xml就可以完成緩衝設定,這樣比較方便。

  4、最後如果以上方法都不行的話,就在你的正常的URL後面加上一個尾巴。

  在JS中就選擇:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->var timestamp = (new Date()).valueOf();
URL+"&timestamp="+timestamp;

  在Java代碼中就選擇:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->long timestamp=new Date().getTime();
URL+"&timestamp="+timestamp;

  這樣的話,你的URL始終都在變化,自然瀏覽器就得老老實實的進行更新了,它也無緩衝可拿了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.