How to avoid using cached data when refreshing the client browser in JSP

Source: Internet
Author: User
Tags browser cache

At work, the following similar section is often seen on the JSP page.Code:

 
Response. setheader ("cache-control", "No-Cache"); // HTTP 1.1 response. addheader ("cache-control", "No-store"); // Firefox response. setheader ("Pragma", "No-Cache"); // HTTP 1.0 response. setdateheader ("expires",-1); response. setdateheader ("Max-Age", 0 );

So what does this code mean? What is the role? See the following explanation:

The functions of the first three lines of the Code are the same, but they are processed to be compatible with multiple browsers. They are used to prevent the browser from caching content dynamically generated by JSP or servlet. The following is an explanation of the original English text:

This is used to prevent the browser from caching your dynamic content generated by a JSP or servlet.
You set this attribute in the HTTP header of the response object which wocould tell the browser not to cache this content. so everytime you request the page again, the browser wocould make a new request, instead of showing you a cached page.

use the server side to control the Ajax page cache:
response. setheader ("Pragma", "No-Cache");
response. addheader ("cache-control", "must-revalidate");
response. addheader ("cache-control", "No-Cache");
response. addheader ("cache-control", "No-store");
response. setdateheader ("expires", 0);
XMLHTTP. setRequestHeader ("cache-control", "No-Cache") is invalid.

Cache-control header field
Cache-control specifies the cache mechanism that requests and responses follow. Setting cache-control in a request message or response message does not modify the cache processing process of another message. The cache commands in the request include no-cache, no-store, Max-age, Max-stale, Min-fresh, only-if-cached, commands in the Response Message include public, private, no-cache, no-store, no-transform, must-revalidate, proxy-revalidate, and Max-age. The instructions in each message are as follows:
Public indicates that the response can be cached in any cache area.
Private indicates that the whole or part of the response message of a single user cannot be processed by the shared cache. This allows the server to only describe part of the user's response message, which is invalid for requests of other users.
No-Cache indicates that the request or response message cannot be cached.
No-store is used to prevent the unintentional release of important information. Sending a request message does not cache the request and response messages.
Max-age indicates that the client can receive responses with a lifetime not greater than the specified time (in seconds.
Min-fresh indicates that the client can receive a response whose response time is earlier than the current time plus the specified time.
Max-stale indicates that the client can receive response messages beyond the timeout period. If the value of the Max-stale message is specified, the client can receive response messages that exceed the timeout period.

Response. setdateheader ("expires", 0 );

Expires is an attribute of response.
It can set the time when the page is saved in the browser cache.
Expired after the specified time
When you browse the page again after expiration, You need to request the server to send page data again.
If you access the page again within the specified time
It does not need to be transferred from the server and directly read from the cache.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.