使用JSP和HTML5进行缓存:如何禁用服务器端缓存 [英] Caching with JSP and HTML5: how to disable caching server-side
问题描述
我有一个返回此html 5的Jsp:
I've a Jsp that returns this html 5:
<html>
<head>
<title>Application</title>
<!-- Some script includes here -->
</head>
<body>
<!-- My html here -->
</body>
</html>
此刻,用户需要禁用浏览器中的缓存,否则每次都会重新加载旧页面.
At the moment the user need to disable caching into the browser, else the old page is reloaded every time.
我试图以这种方式强制使用scriptlet进行不缓存,但是没有成功:
I tried to force no-caching with a scriptlet in that way, but without success:
<%
response.addHeader("Cache-Control","no-cache");
response.addHeader("Expires","-1");
response.addHeader("Pragma","no-cache");
%>
事实证明scriptlet并不是一个好的解决方案,在JSP中有什么方法可以禁用缓存?
Asde the fact the scriptlet wouldn't be a good solution, is there any way that works in JSP to disable caching?
推荐答案
鉴于您使用的是在Web容器中运行的jsp文件.为此,我们使用javax.servlet.Filter
设置标头值.
Given you are using jsp files you are running this in a web container. We do this by using a javax.servlet.Filter
to set the header values.
我不知道任何开源过滤器已经做到了,但是编写自己并不困难.
I don't know of any open sources filters that already do this but it is not that difficult to write yourself.
我们为HTTP/1.0设置的标头:
The headers we set for HTTP/1.0:
httpResponse.setDateHeader("Expires", 0L);
httpResponse.setHeader("Pragma", "no-cache");
我们为HTTP/1.1设置的标头:
The headers we set for HTTP/1.1:
httpResponse.setHeader("Cache-Control", "private,no-store,no-cache");
这篇关于使用JSP和HTML5进行缓存:如何禁用服务器端缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!