How to prevent caching of servlet result?

How to stop page caching in a browser using a servlet?

I want this session to end if I click the back button of the browser when I am logged in.

+4
source share
2 answers

To permanently disable the cache.

// Set to expire far in the past. response.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT"); // Set standard HTTP/1.1 no-cache headers. response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); // Set IE extended HTTP/1.1 no-cache headers (use addHeader). response.addHeader("Cache-Control", "post-check=0, pre-check=0"); // Set standard HTTP/1.0 no-cache header. response.setHeader("Pragma", "no-cache"); 

Clearing the client’s cache will not expire the session immediately, but it will clear the session cookies in the browser. To immediately end a session, you need to explicitly specify the jsp or servlet server side.

 // use session invalidate session.invalidate(); 
+10
source

If you get an HttpServletResponse object (implementation) for the request, you can send HTTP headers that will encourage browsers not to cache the content you send them.

 HttpServletResponse response; // You'll need to initialize this properly response.setHeader("Cache-control", "no-cache, no-store"); response.setHeader("Pragma", "no-cache"); response.setHeader("Expires", "-1"); 

See the documentation for HttpServletResponse and HttpServletResponseWrapper . If you need to read cache control headers in HTTP, check this out .

+2
source

Source: https://habr.com/ru/post/1341611/


All Articles