Caching with JSP and HTML5: how to disable caching server-side

此生再无相见时 提交于 2021-02-20 04:49:42

问题


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.

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");
%>

Asde the fact the scriptlet wouldn't be a good solution, is there any way that works in JSP to disable caching?


回答1:


Cache-Control

The above header must be a cross browser one.Might that causing problems

Try

response.addheader('Cache-Control: no-cache, no-store, must-revalidate');



回答2:


If you are using Apache Tomcat change context.xml

<Context cachingAllowed="false">

You can read the documentation at http://tomcat.apache.org/tomcat-6.0-doc/config/context.html which says for

cachingAllowed

If the value of this flag is true, the cache for static resources will be used. If not specified, the default value of the flag is true.




回答3:


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.

The headers we set for HTTP/1.0:

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

The headers we set for HTTP/1.1:

httpResponse.setHeader("Cache-Control", "private,no-store,no-cache");


来源:https://stackoverflow.com/questions/18982359/caching-with-jsp-and-html5-how-to-disable-caching-server-side

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!