Junior — Middle
How can data caching be controlled and configured in a browser when processing a specific request?
sobes.tech AI
Answer from AI
Caching control in the browser during processing of a specific HTTP request is managed through server response headers. The main headers for cache management are:
Cache-Control: sets caching rules (e.g.,no-cache,no-store,max-age=3600).Expires: indicates the date and time until which the response is considered fresh.ETag: a unique identifier for the resource version, allowing the browser to check for changes.Last-Modified: the date of the last modification of the resource.
Example of setting headers in Java (for instance, in a servlet):
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
// then send the response body
}
This approach guarantees that the browser will not use outdated cache data and will always request the latest version from the server.
For more fine-tuned control, you can use ETag and handle the If-None-Match request header to return 304 Not Modified if the resource has not changed.