user1883212
user1883212

Reputation: 7859

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

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?

Upvotes: 1

Views: 2289

Answers (3)

tom
tom

Reputation: 2745

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

Upvotes: 0

AurA
AurA

Reputation: 12373

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.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122026

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');

Upvotes: 1

Related Questions