【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+"×tamp="+timestamp;
在Java代碼中就選擇:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->long timestamp=new Date().getTime();
URL+"×tamp="+timestamp;
這樣的話,你的URL始終都在變化,自然瀏覽器就得老老實實的進行更新了,它也無緩衝可拿了。