12

How to disable caching ?

What headers should doGet set?

Could you provide a code snippet?

EugeneP
  • 11,783
  • 32
  • 96
  • 142

2 Answers2

28

This will set caching to disabled on the response:

// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "private, no-store, no-cache, must-revalidate");

// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
rsp
  • 23,135
  • 6
  • 55
  • 69
  • 5
    I believe you would like to add `response.setDateHeader("Expires", 0);` for proxies. See another [answer](http://stackoverflow.com/questions/3413036/http-response-caching) – mvnukov Jan 25 '13 at 11:49
  • Which servlet should we write this code into? the one that is redirecting to the html page keeping the cache? – Fakipo Sep 10 '22 at 00:04
0

I want to disabled the varnish cache if backend server return some header

eg.

if is set header like follow

JAVA code

ServletActionContext.getResponse().addHeader("ABC","true")

then in varnish if I found value of header ABC as true then I don't want to cache this page.

RAS
  • 8,100
  • 16
  • 64
  • 86
Alpesh Jikadra
  • 1,692
  • 3
  • 18
  • 38