浏览器通常会缓存页面的内容
但我们在开发阶段,不希望浏览器去缓存页面内容!
在jsp或html中添加如下代码就禁用缓存
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
在servlet中添加如下代码就禁用缓存
response.addHeader("pragma", "no-cache");//HTTP/1.0
response.addHeader("cache-control", "no-cache");//HTTP/1.1
response.addHeader("expires", "-1");//缓存的时间为-1
如果资源是css或js,可能我们就缓存了!
但我们在开发阶段,不希望浏览器去缓存页面内容!
在jsp或html中添加如下代码就禁用缓存
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
在servlet中添加如下代码就禁用缓存
response.addHeader("pragma", "no-cache");//HTTP/1.0
response.addHeader("cache-control", "no-cache");//HTTP/1.1
response.addHeader("expires", "-1");//缓存的时间为-1
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse resp = (HttpServletResponse) response;
HttpServletRequest req = (HttpServletRequest) request;
// 获取访问的URI
String uri = req.getRequestURI();
// 判断访问的资源如果不是.css也不是.js,那么禁用缓存!
if(!uri.equalsIgnoreCase(".css") && !uri.equalsIgnoreCase(".js")) {
resp.addHeader("pragma", "no-cache");//HTTP/1.0
resp.addHeader("cache-control", "no-cache");//HTTP/1.1
resp.addHeader("expires", "-1");//缓存的时间为-1
}
chain.doFilter(request, response);
}
如果资源是css或js,可能我们就缓存了!