JSP技术分享:常用禁止缓存的四种方法

【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始终都在变化,自然浏览器就得老老实实的进行更新了,它也无缓冲可拿了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值