如何控制JSP页面中的缓存? [英] How to control cache in JSP page?

查看:65
本文介绍了如何控制JSP页面中的缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在doFilter中使用以下代码创建了一个Servlet过滤器:

I created a Servlet filter with the following code in doFilter:

HttpServletResponse httpResponse = (HttpServletResponse)response;

httpResponse.setHeader("Cache-Control","no-cache");
httpResponse.setHeader("Pragma","no-cache");
httpResponse.setDateHeader("Expires", 0); 

chain.doFilter(request, response);

我想确保没有任何内容缓存在客户端上,并且每个请求(甚至是浏览器后退按钮上的请求)都定向到服务器.

I want to make sure that nothing gets cached at the Client and every request (even the one's from the Browser's back button) are directed to the Server.

但是,即使在实现上述过滤器之后,也会缓存一些页面(可使用浏览器的后退按钮访问).

But, even after implementing the above filter, some pages are cached (accessible using browser's back button).

和其他未缓存的页面,在Internet Explorer中显示网页过期错误.

And other pages that are not cached, show Web Page Expired error in Internet Explorer.

推荐答案

首先,完整的集是:

httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.
httpResponse.setDateHeader("Expires", 0); // Proxies.

需要no-storemust-revalidate才能使其在每个Firefox下都可以正常工作.

The no-store and must-revalidate are required to get it to work in under each Firefox.

但是,即使实施了上述过滤器,也会缓存某些页面(可使用浏览器的后退按钮访问).

您是如何测试的?这些标头实际上将阻止浏览器从浏览器缓存而不是直接从服务器请求页面.最好的测试是让Filter侦听/*并添加以下风味的调试语句:

How did you test it? Those headers will actually prevent the browser from requesting the page from the browser cache instead of directly from the server. Best test is to have a Filter to listen on /* and add a debug statement in flavor of:

HttpServletRequest httpRequest = (HttpServletRequest) request;
String method = httpRequest.getMethod();
String URI = httpRequest.getRequestURI();
System.out.println(method + " request invoked on " + URI);

这应该打印实际的请求.

This should print the actual requests.

还要确保您不要使用HTML <meta>标记覆盖JSP页面本身中的标题.

Also ensure that you don't override the headers in the JSP page itself using the HTML <meta> tags.

和其他未缓存的页面,在Internet Explorer中显示网页过期错误.

And other pages that are not cached, show Web Page Expired error in Internet Explorer.

仅当非缓存请求是POST请求而不是GET请求时,您才能获得此请求. GET请求将仅再次从服务器而不是从浏览器缓存中请求.

You can only get this if the non-cached request was POST request, not a GET request. The GET requests will simply be requested from server again instead of from the browser cache.

这篇关于如何控制JSP页面中的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆