The Settings page in Web development disables caching __web

Source: Internet
Author: User
Tags browser cache
Browser Cache Insider and solution

In the following three different scenarios, browsers have different caching conditions:
1). The servlet does not overwrite the Getlastmodified method, there is no LastModified header field in the response message, and there is no "last modified time" in the browser-cached document.
2. There is a getlastmodified method, the response message has the LastModified header field, but the return time is later than the cached document "last modified"
3. There is a getlastmodified method, the response message has the LastModified header field, but the return time is equal to the cached document "Last Modified Time"
Back, Forward, Goto (press ENTER after manual entry) to refresh via hyperlink access
1) do not send a request, use the cache issue request directly
2) No request, direct use of the cache does not send a request to send a request, return 200
3) No request, direct use of the cache does not send a request to send a request, return 304
2, how to prevent the servlet cache.

Java code

Response.setdateheader ("Expires", 0);    
Response.setheader ("Cache-control", "No-cache");    
Response.setheader ("Pragma", "No-cache");    
Response.setdateheader ("Expires", 0);  
Response.setheader ("Cache-control", "No-cache");  

3, how to prohibit the JSP page cache.

HTML code

Response.setdateheader ("Expires", 0);    
Response.setheader ("Cache-control", "No-cache");    
Response.setheader ("Pragma", "No-cache");    
    
    
< http-equivhttp-equiv= "Pragma" content= "No-cache" >    
< http-equivhttp-equiv= "Cache-control" content= " No-cache ">    
< http-equivhttp-equiv=" Expires "content=" 0 ">    
response.setdateheader (" expires ", 0);  
Response.setheader ("Cache-control", "No-cache");  
Response.setheader ("Pragma", "No-cache");  
 
 
< http-equiv= "Pragma" content= "No-cache" >  
< http-equiv= "Cache-control" content= "No-cache" >  



4, how to prevent the static page cache. (Page No. 238 of Mr. Zhang's Javaweb book)

HTML code
< http-equivhttp-equiv= "Pragma" content= "No-cache" >    
< http-equivhttp-equiv= "Cache-control" content= " No-cache ">    
< http-equivhttp-equiv=" Expires "content=" 0 ">    
< http-equiv=" Pragma "content=" No-cache ">  
< http-equiv=" Cache-control "content=" No-cache ">  
< http-equiv=" Expires "content=" 0 " >

After the static page is blocked, refresh the browser to return 304
After the JSP page is blocked, refresh the browser to return 200


Research on caching problem in web development
In general, the browser will cache the content of the page has been visited, about how to prevent browser caching, the introduction of the Internet everywhere there are related articles, but the browser how to use caching, how to deal with caching, but few people talk about. I've always been puzzled by this question, which is also the problem that most experienced web developers have, and some of my friends have done dozens of of web projects that are large and small, and they have encountered and solved these problems in their projects when they are interacting with them, But because there is not enough time and energy to think carefully about the causes and details of these problems, they have always been a smattering of these questions, vague, and there are few specific and detailed explanations of these issues, I recently spent two days, the browser caching problem thoroughly studied a turn, Mainly include details of the aspects.

1 How to prohibit browser caching, this is the simplest question, is ashamed to explain here, but for the sake of completeness, it may be listed as a point of knowledge.

2 when a browser accesses a cached resource, under what circumstances it sends a request to the server. Under what circumstances the request will not be sent to the server at all. This is related to the browser's caching settings! However, because almost everyone's browser is the default setting, the focus should be on analyzing the browser's default caching settings.

3 whether the browser will issue an access request to the server when accessing a cached resource through a hyperlink in another Web page document. If not, an issue arises: when you sell a product and then return to the display page of the inventory, you will see what you saw earlier, not the updated inventory data. However, when accessing an ordinary HTML file, if the browser sends an access request to the server every time, the efficiency is relatively low, which loses the meaning and value of the cache. Therefore, the conclusion should be browser access to dynamic pages can not use the cache, while access to static pages should use the cache, but only according to the access to the page's resource name, the browser is unable to know whether the inventory Display page is dynamic content, or static content. The browser is based on what way to determine whether its cached resources are dynamic, or static. Under what request does it always make a new request to the cached resource?

4 for cached content, even if the browser sends a request to the server, the server may not return the content after receiving the request, but instead let the browser continue to use the cached content, which is useful in practical applications. How to deal with its specific details.

5 server side also has the cache, when the server received the browser request, assume that it returned the response content, but the return of the response content may not be the latest content, but most likely an old cached version, this is what happened.
Browser Cache Insider and Getlastmodified method
A getlastmodified method is defined in the HttpServlet class, and its complete syntax is defined as follows:
Protected long getlastmodified (HttpServletRequest req)
The return value represents the number of milliseconds to calculate starting at 0 points 0 minutes and 0 seconds from January 1, 1970, and the Getlastmodified method defined in the HttpServlet class always returns a negative, which can be overridden in a httpservlet subclass. In order to return a modification time that represents the response content of the current output, the HttpServlet class's service method can automatically generate Last-modified header fields in response messages based on this return value.

In general, browsers cache the content of the page that has been visited, and the return value of the Getlastmodified method can affect how the browser handles and utilizes cached content. Before you learn more about the application of the Getlastmodified method, you should understand the caching mechanism of the browser.

Click the IE Tools Internet Options menu, open the Internet Options dialog box, and then click the Settings button in the Temporary Internet Files column on the General tab to open the Settings dialog box. The Settings dialog box, in the Temporary Internet folder bar, you can see the full directory name of the folder where the browser saves all cached page content.

The check every time the page is accessed option indicates that each time the browser accesses a page, it sends an access request to the server, regardless of whether the browser has cached the page. The advantage of this setting is that it is very real-time and will definitely be able to access the latest content of the Web page, but this setting is less efficient if the content of the Web page is rarely updated.

The "Check every time you start Internet Explorer" option means that when you first access a page during each startup of the browser, an access request is made to the server, regardless of whether the browser has cached the page, but subsequent access to the page during this startup of the browser, Instead of making an access request to the server, the browser will use the contents of the cache directly. This setting has a high access efficiency, but also take into account a good real-time, it can ensure that every time you start the browser to see the latest Web content.

The "Auto" option is similar to the "Check every time you start Internet Explorer" option, except that the image is accessed differently, and if over time, the browser finds that the image on the page is not updated frequently, so Even if the browser makes its first access to a cached image after performing this startup, it does not necessarily send an access request to the server, but simply uses the contents of the cache directly. The auto option is the default setting for browsers, so almost everyone's browsers work this way, and the role and significance of this option should be a familiar focus for readers.

The "Do not check" option means that whenever a browser accesses a page, the browser does not issue an access request to the server, as long as it can locate the cached information locally, but instead uses the cached content directly. The advantage of this setting is that access is efficient, but if the server-side Web page content is updated, what the browser sees is likely to be outdated content.

In the case of the browser's "Check for newer versions of saved pages" feature item with the default "auto" setting, if the browser has just visited a Web page, the server side has updated the content of this page, when browsing before the shutdown and again visit the page, the user will not see the updated content of the page, Instead, the content of the page is expired. To improve browsing efficiency, when accessing static Web page content, such a small probability of expiration information should be allowed, and these outdated information does not cause any bad consequences, as you occasionally see the day before the news, rather than the news of today, what is the problem? However, if the browser is accessing a Dynamic Web page, this would have required the browser to see the latest content on every visit during its entire run, for example, when you sell a product and then return to the display page of the inventory, you should see updated inventory data instead of what you saw earlier. Depending on the resource name of the page being accessed, the browser cannot tell whether the display page of the inventory is dynamic or static. In this case, the browser will be processed according to whether the Last-modified header field is included in the response message, and if the response message does not contain a last-modified header field, it will issue an access request to the server each time the page is accessed, otherwise, It only makes an access request to the server when the page is first accessed every time the run is started, and subsequent access to the page is no longer issued to the server during the startup run.

The Last-modified header field in the response message can be used to specify the last update time of the response content, which, when the client caches the contents of the document, will be based on the Last-modified the time specified by the header field to generate the If-modified-since Request header field to indicate the last update time of the cached document. The server returns the document content only if the document was modified more than the time specified by the If-modified-since request header. If the content of the Web page has not been modified since if-modified-since specified time, the server returns a 304 (not Modified) status code to indicate that the browser-cached version is up to date without returning the document content to the browser. The browser continues to use the previously cached content. In this way, the communication data between the browser and the server can be reduced to some extent, which improves the communication efficiency.

The HttpServlet class provides a processing mechanism for this application of the If-modified-since request header and Last-modified header fields, when the servlet program that inherits the HttpServlet class receives a get-way access request, The overloaded service method in HttpServlet will also invoke the Getlastmodified method before calling the Doget method. And based on the return value of the Getlastmodified method, decide whether to call the Doget method and whether to generate the Last-modified header field in the response message, as follows:

When the Getlastmodified method returns a negative number, regardless of the situation in the request message, the service method calls the Doget method directly to generate the response content, which is exactly the behavior of the Getlastmodified method defined in the HttpServlet class;

When the Getlastmodified method returns a positive number and the request message does not contain a If-modified-since request header (which often occurs on the first visit to a resource), or the time value in the If-modified-since request header contained in the request message is older than the time value returned by the Getlastmodified method. The service method generates a Last-modified header field based on the return value of the Getlastmodified method, and then invokes the Doget method to generate the response content;

When the Getlastmodified method returns a positive number, and the time value in the If-modified-since request header contained in the request message is newer or the same as the time value returned by the Getlastmodified method, Instead of calling the Doget method, the service method returns a 304 (not Modified) status code to the browser indicating that the browser can use its previously cached content.


Disable caching using the filter configuration


Java code
Nocachefilter.class
Import javax.servlet.*;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;


public class Nocachefilter implements Filter {public


    void Destroy () {

    }

    //browser does not cache page public
    void Dofilter (ServletRequest request, servletresponse response,
            Filterchain chain) throws IOException, servletexception{
       /Set message headers that prohibit caching
       ((httpservletresponse) response). SetHeader ("Pragma", "No-cache");     
       ((httpservletresponse) response). SetHeader ("Cache-control", "No-cache");     
       ((httpservletresponse) response). SetHeader ("Expires", "0");/disable caching     
       
       chain.dofilter (request, response);
       System.out.print ("Nocachefilter");
    }

    public void init (Filterconfig arg0) throws servletexception {
    }

}


Web.xml Configuration

<filter> 
<filter-name>NoCacheFilter</filter-name> 
<filter-class> com.huadi.tools.nocachefilter</filter-class> 
</filter> 
<filter-mapping> 
< filter-name>nocachefilter</filter-name> 
<url-pattern>/servlet/*</url-pattern> 
</filter-mapping> 
<filter-mapping> 
<filter-name>NoCacheFilter</filter-name> 
<url-pattern>*.jsp</url-pattern> 
</filter-mapping>




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.