Research on cache in Web Development (2)

Source: Internet
Author: User

Research on cache in Web Development (I)
Research on cache in Web Development (2)
Research on cache in Web Development (III)

As shown in figure 4.16, there are four options for setting the function item "Check newer versions of existing webpages". You only need to click the question mark button in the title bar of the "Settings" dialog box, then click the corresponding options to see the role and significance of each option:

  • The "check each time you access this page" option indicates that the browser sends an access request to the server regardless of whether the browser has cached this page. The advantage of this setting is that it is highly real-time and can certainly access the latest content on the webpage. However, if the content of the webpage is rarely updated, the access efficiency of this setting is relatively low.
  • The "check when Internet Explorer is started" option indicates that during each startup of the browser, When you access a page for the first time, whether or not the browser caches the page, all requests must be sent to the server. However, when the browser starts to run this time, the browser will not send access requests to the server, but directly use the cached content. This setting has a high access efficiency, while also taking into account the good real-time, it can ensure that each time you start the browser to see the latest web page content.
  • The "automatic" option is similar to the "check when Internet Explorer is started" option, but the access to images is different. If the time passes, the browser finds that the images on the Web page are not updated frequently, so that even if the browser performs the first access to a cached image since this startup, it does not necessarily send an access request to the server, but simply uses the cached content directly. The "automatic" option is the default setting of the browser. Therefore, almost all browsers work in this way. The role and significance of this option should become the focus of the reader's familiarity.
  • The "do not check" option indicates that the browser will not send an access request to the server as long as the cache information of this page can be found locally when accessing a page under any circumstances, instead, the cached content is used directly. The advantage of this setting is high access efficiency. However, if the webpage content on the server is updated, the content displayed by the browser may be expired.

When the default "automatic" setting item is used for the function item "Check the newer version of the existing web page" of the browser, if the browser has just accessed a Web page, the server updates the content of this webpage. When you access this page again before closing the page, you will not see the updated webpage content, but the expired webpage content. In order to improve browsing efficiency, such a small probability of expiration information should still be allowed when accessing static webpage content, and such expiration information will not cause any bad consequences, just as you occasionally see the news from the previous day, rather than the news of the current day, what's the problem? However, if a browser accesses a dynamic web page, the browser must be able to see the latest content each time during its entire operation. For example, when a product is sold and then returned to the product inventory display page, the updated inventory data is displayed instead of the previously seen content. Based on the Resource Name of the accessed page, the browser cannot know whether the display page of the product inventory is dynamic or static. In this case, the browser processes the request based on whether the response message contains the last-modified header field. If the response message does not contain the last-modified header field, it sends an access request to the server each time it accesses this page. Otherwise, it sends an access request to the server only when it first accesses this page after each start operation, however, no subsequent access requests to this page will be sent to the server during startup and running.
As described in Chapter 2nd, the last-modified header field in the response message can be used to specify the last update time of the response content. After the client caches the content of this document, in future request messages, it will generate the IF-modified-since request header field based on the time specified by the last-modified header field to indicate the last update time of the cache document. The server returns the document content only when the document modification time is later than the time specified by the IF-modified-since request header. If the webpage content has not been modified since the time specified by if-modified-since, the server returns a 304 (not modified) status code to indicate that the browser cache version is the latest, without returning the document content to the browser, the browser continues to use the previously cached content. In this way, the data volume between the browser and the server can be reduced to a certain extent, thus improving the communication efficiency.
The httpservlet class provides a processing mechanism for such applications as the IF-modified-since request header and the last-modified header field, when the servlet program inheriting the httpservlet class receives a get access request, the HTTP servlet overload service method will call the getlastmodified method before calling the doget method, determine whether to call the doget method and whether to generate the last-modified header field in the Response Message Based on the returned value of the getlastmodified method. The specific rules are as follows:

  • When the getlastmodified method returns a negative number, regardless of the situation in the request message, the service method will directly call the doget method 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 the IF-modified-since Request Header (this usually occurs during the first access to a resource ), or when the time value in the Request Header contained in the request message is earlier 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, then call the doget method to generate the response content;
  • When the getlastmodified method returns a positive number, and the time value in the Request Header contained in the request message is new or consistent with 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: uncover the mysteries of browser cache

(1) Compile a servlet program named cacheservlet and print the current time value to the command line window of the browser and tomcat in its doget method, the getlastmodified method also prints the current time value and returns the current time value to the Tomcat command line window. Here, comment out the getlastmodified method first, as shown in Example 4-9.

Example 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 make sure that the class file generated after compilation is placed in the D:/myweb/WEB-INF/classes directory.

(2) Compile a webpage file named cachetest.html in D:/mywebdirectory, as shown in Figure 4-10.

Example 4-10 cachetest.html

<A href = "Servlet/cacheservlet"> cache test </a>

(3) To facilitate viewing how the browser generates cached content, it is best to delete all cached content saved in the Temporary Internet folder first. Click the "Tools" and "Internet Options" menu of IE browser to open the "Internet Options" dialog box shown in 4.16, then, click the "delete file" button in the "Temporary Internet Files" column on the "General" tab, which deletes all the cached content saved in the Temporary Internet folder. Click the "Settings" button in the "Temporary Internet Files" column on the "General" tab. In the "Settings" dialog box that appears, click the "view files" button in the "Temporary Internet Folders" column, open the Temporary Internet folder shown in 4.17 and you can see that there are no cached files. In addition, in the "Settings" dialog box, you also need to restore the function item "Check newer versions of existing webpages" to the default "automatic ".
Enter the following address in the browser address bar:
HTTP: /localhost: 8080/it315/cachetest.html
On the results page displayed in the browser window, click the "cache test" hyperlink to access cacheservlet. Open the internetbench folder again, and you can see that the cache files of the previous pages cachetest.html and cacheservlet are generated, as shown in 4.18. Select the cacheservlet cache file. The summary information displayed in the Windows Resource Manager window shows that the last modification time recorded in the cacheservlet cache file is "NONE ".


Fig 4.18

(4) execute the Telnet 127.0.0.1 8080 command in the command line window, connect to the Tomcat web server, and then enter the following content in the Telnet command window:
GET/it315/servlet/cacheservlet HTTP/1.1 <press enter>
HOST: <space> <press enter>
<Press enter>
The result 4.19 is displayed in the Telnet window.


Fig 4.19

As shown in figure 4.19, no last-modified header field exists in the Response Message returned by cacheservlet. This is why the last modification time of the cacheservlet cache file shown in Figure 4.18 is "NONE.

Research on cache in Web Development (I)
Research on cache in Web Development (2)
Research on cache in Web Development (III)

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.