JSP惯用禁止缓存的四种方法
以下是常用禁止缓存的四种方法:
方法一:
客户端缓存要在head中加入类似如下内容:
<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"
>
或
<
meta
http-equiv
="pragma"
content
="no-cache"
>
<
meta
http-equiv
="cache-control"
content
="no-cache"
>
<
meta
http-equiv
="expires"
content
="0"
>
方法二:
在服务端的动态网页中禁止缓存,要加入类似如下脚本:
response.setHeader(
"
Pragma
"
,
"
No-cache
"
);
response.setHeader(
"
Cache-Control
"
,
"
no-cache
"
);
response.setDateHeader(
"
Expires
"
,
0
);
方法三:
设置有限时间的缓存:
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就可以完成缓冲设置,这样比较方便。
方法四:
时间戳:
var timestamp
=
(
new
Date()).valueOf();
URL
+
"
×tamp=
"
+
timestamp;
在每次提交的url上跟上一个随机变化的时间,浏览器认为每次提交都是不同的url,自然会每次刷新当然返回结果。