By setting properties that prevent caching in your JSP Page.They are:
<%
response.setHeader(“pragma”,”no-cache”);//HTTP 1.1
response.setHeader(“Cache-Control”,”no-cache”);
response.setHeader(“Cache-Control”,”no-store”);
response.addDateHeader(“Expires”, -1);
response.setDateHeader(“max-age”, 0);
//response.setIntHeader(“Expires”, -1);//prevents caching at the proxy server
response.addHeader(“cache-Control”, “private”);
%>
To prevent the browser from caching data of the pages you visit in a web application developed with Advance Java, you can use various techniques. Here are a few approaches:
- HTTP Headers: You can set appropriate HTTP headers in the response to instruct the browser not to cache the content. For example, you can include the following headers in your server-side code:
java
HttpServletResponse response = ... // Obtain the HttpServletResponse object
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "0");
These headers collectively indicate to the browser that it should not store a cached copy of the page, must not use a cached copy without revalidating it, and the content is considered stale immediately.
- Adding a Random Query Parameter: Another common technique is to add a random query parameter to the URLs of your resources. This way, each request is unique, and the browser is less likely to serve a cached version of the resource. For example:
java
<link rel="stylesheet" href="/css/style.css?v=123" />
In this example, the
v=123
query parameter can be changed whenever you want to force the browser to fetch a fresh copy of the stylesheet. - Versioning URLs: You can version your static resource URLs. By including a version number or timestamp in the URL, you ensure that the browser recognizes a change in the resource and requests the updated version.
java
<link rel="stylesheet" href="/css/style-v1.2.3.css" />
When you make changes to the CSS file, you update the version number in the URL.
Choose the approach that best fits your application’s requirements. It’s common to use a combination of these techniques for comprehensive cache control.