Research on the caching problem in J2eeweb development __web

Source: Internet
Author: User
Tags browser cache java web
Research on the caching problem in the development of J2eeweb

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 the cache, how to deal with the caching of the explanation, but few people talk about. I've been puzzled by this question, This is also a problem for the vast majority of experienced web developers, and some of my friends have done dozens of of large and small web projects, and when communicating these issues to them, they have encountered and solved them in their projects, 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, which is the simplest question, is shy to explain, but for completeness, it may be a point of knowledge.

2. When a browser accesses a cached resource, under what circumstances will it send a request to the server? Under what circumstances does the request 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, Emphasis should be placed on the study of analyzing the default caching settings for browsers.

3. When you access a cached resource through a hyperlink in another Web page document, does the browser send an access request to the server? 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. So, the conclusion is that browsers cannot use caching when accessing dynamic pages, while accessing static pages should use caching, but 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. What is the way the browser determines whether its cached resources are dynamic or static? Under what request does it always make new requests for cached resources?

4. For cached content, even if the browser sends a request to the server, but the server may not return the content after receiving the request, but instead let the browser continue to use the cached content, what is the benefit of the actual application? How to deal with its specific details?

5. The server side also has the cache, when the server received the browser request, assume 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?

All these problems, in the author's "In-depth experience of the inside of Java Web Development," a book has profound analysis and detailed experimental steps.

The following is an excerpt from the book (excluding server-side caching techniques, and server-side caching techniques are analyzed in other chapters)

4.5.8 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.
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 as shown in Figure 4.16. 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 "Tools" in IE Explorer
You can also see that there are 4 options for setting the "Check for newer versions of saved pages" feature item, as long as you click the question mark button in the title bar of the Settings dialog box, and then click the appropriate option to see the action and meaning of each option:

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.
As explained in the 2nd chapter, the Last-modified header field in the response message can be used to specify the last update time of the response content, and when the client caches the contents of the document, It generates the If-modified-since Request header field in a later request message based on the time specified by the Last-modified 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.
Hands-on experience: Unlocking the secrets of browser caching
(1) Write a servlet program called Cacheservlet that prints the current time value to the browser and Tomcat's command line window in its Doget method. The Getlastmodified method also prints out the current time value and returns the current time value to the Tomcat Command Line window, where the Getlastmodified method is commented out, as shown in routine 4-9.

Routine 4-9 Cacheservlet.java

Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;

Public class Cacheservlet extends HttpServlet
{
   public void doget (HttpServletRequest request,
     httpservletresponse response) throws Servletexception, IOException
   {
      PrintWriter out = Response.getwriter ();
      Long now = System.currenttimemillis ();
      out.println ("doget:" + now);
      System.out.println ("doget:" + now);
  }
 
   /*protected long getlastmodified (httpservletrequest req)
      {
        long now = System.currenttimemillis ();
        System.out.println ("getlastmodified:" + now);
        return now;
      }*/
}

Compile the Cacheservlet.java file to ensure that the compiled class file is placed in the D:/myweb/web-inf/classes directory.

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.