JSP禁用缓存常用方法

MusicPeng 2013-10-24

JSP禁用缓存常用方法

jsp页面禁止缓存设置

1.客户端缓存要在<head>中加入类似如下内容:

<METAHTTP-EQUIV="pragma"CONTENT="no-cache">

<METAHTTP-EQUIV="Cache-Control"CONTENT="no-cache,must-revalidate">

<METAHTTP-EQUIV="expires"CONTENT="Wed,26Feb199708:21:57GMT">

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

2.在服务器的动态网页中禁止缓存,要加入类似如下脚本

response.setHeader("Pragma","No-cache");

response.setHeader("Cache-Control","no-cache");

response.setDateHeader("Expires",0);

3.设置有限时间的缓存

intminutes=10;

Dated=newDate();

StringmodDate=d.toGMTString();

StringexpDate=null;

expDate=(newDate(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

建议:jspcache最好做在过滤器上,把需要缓冲的页面集中在同一个目录下,每次更改只须更改web.xml就可以完成缓冲设置,这样比较方便.

相关推荐