Browser cache insider and getlastmodified Method

Source: Internet
Author: User
Tags browser cache
The httpservlet class defines a getlastmodified method. Its complete syntax is defined as follows:
Protected long getlastmodified (httpservletrequest req)
The returned value indicates the number of milliseconds that have been calculated since 00:00:00, January 1, January 1, 1970. The getlastmodified method defined in the httpservlet class always returns a negative number, which can be overwritten in the httpservlet subclass, in order to return a modification time that represents the current output of the response content, the service method of the httpservlet class can automatically generate the last-modified header field in the Response Message Based on the returned value.
Generally, the browser caches the accessed page content. The returned value of the getlastmodified method affects how the browser processes and uses the cached content. Before learning more about the application of the getlastmodified method, you should first understand the browser cache mechanism. Click the "Tools" and "Internet Options" menu in IE, open the "Internet Options" dialog box, and then click "Settings" in the "Temporary Internet Files" column on the "General" tab, open the "Settings" dialog box shown in 4.16.

Fig 4.16

In the "Temporary Internet folder" column of the "Settings" dialog box shown in Figure 4.16, you can see the complete Directory Name of the folder in which the browser saves all cached page content. For administrator users, the default setting is "C:/Documents and Settings/Administrator/Local Settings/Temporary Internet Files ". Click the "view files" button in the "Temporary Internet Folders" column to open this folder, as shown in Figure 4.17 ..

 

Fig 4.17

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.

(5) return to the browser window and remember the time value displayed in the browser window and tomcat command line window, click the "back" and "Forward" buttons in the toolbar of the browser repeatedly. You can see that the content of the cacheservlet page is not changed each time, and no new information is printed in the Tomcat command line window. Enter the access address of the cacheservlet in the address bar of the browser. This indicates that when you access a cached page in the browser's "back" or "Forward" mode, or directly access the cached page in the browser's address bar, the browser directly calls the cached content without sending a new access request to the server.
Delete the cache file on the cacheservlet page in the Windows Resource Manager window shown in Figure 4.18, and use the "back" and "Forward" buttons again to access the cacheservlet, alternatively, enter the access address of the cacheservlet in the address bar of the browser. A new time value is displayed in both the browser window and the command line window of Tomcat. This indicates that when the cached file no longer exists, you can use the "back" and "Forward" buttons to access the cacheservlet, or directly enter the access address of the cacheservlet in the address bar of the browser, the browser sends a new access request to the server.
Go back to the cachetest.html page from the browser toolbar or click the hyperlink to access the cacheservlet. A new time value is displayed in the browser window and tomcat command line window. Repeat this process to see that the cacheservlet page displays a new time value each time. This means that when the cacheservlet is called through hyperlinks in other Web documents, the browser will send a new access request to the server, instead of calling cached content.
(6) modify the cacheservlet. Java source file, uncomment the getlastmodified method, recompile the cacheservlet. Java source file, wait for Tomcat to re-load the cacheservlet, and refresh the access to the cacheservlet in the browser. Open the Temporary Internet folder (if the folder has already been opened, press F5 to refresh it) and select the cacheservlet cache file, the summary information displayed in the Windows Resource Manager window shows that the cacheservlet cache file contains a record of the last modification time, as shown in Figure 4.20.


Fig 4.20

(7) Repeat Step (4). At this time, the Telnet window shows the result 4.21.


Fig 4.21

As shown in figure 4.21, a last-modified header field is added to the response message returned by cacheservlet, this is exactly why the cacheservlet cache file shown in Figure 4.20 has a record of the last modification time.
(8) return to the browser window and remember the time value displayed in the browser window and tomcat command line window, click the "back" and "Forward" buttons in the toolbar of the browser repeatedly. You can see that the content of the cacheservlet page is not changed each time, and no new information is printed in the Tomcat command line window. Enter the access address of the cacheservlet in the address bar of the browser. This is exactly the same as the experiment shown in step (5.
Go back to the cachetest.html page from the browser toolbar or click the hyperlink to access the cacheservlet. Then, you can see that the content displayed in the browser window and tomcat command line window remains unchanged. repeat this process multiple times, the result remains unchanged. This situation is quite different from the experiment results shown in step (5). We can draw the following conclusions by comparing the experiment results:

  • If the response message of a page contains the last-modified header field, when the page is called through a hyperlink in another webpage document, the browser sends an access request to the server only when the page is accessed for the first time after each startup, the browser will not send an access request to the server, but directly call the cached content. When accessing a common HTML file, Tomcat's default servlet will generate a last-modified header field to indicate the latest modification time of the HTML file. Therefore, during a browser startup and running, it sends a real access request to the server only when it first accesses an HTML file. For an HTML page, the content of the page changes between the first and subsequent accesses to the page during a certain running period of a browser, even if this happens, there will be no major problems. Therefore, the default servlet of Tomcat is completely reasonable and appropriate.
  • If the response message of a page does not contain the last-modified header field, the browser sends an access request to the server for each access to the page during the entire startup and running process without calling the cached content. Therefore, as long as the last-modified header field is not generated in the dynamic web page program, you do not have to worry about accessing the dynamic web page program through a hyperlink.

Refresh the access to the cacheservlet in the browser window. The refreshing function is to make sure that the browser sends an access request to the server. At this time, the browser window displays new content. View the command line window for starting tomcat. The following two lines of new information are displayed:
Getlastmodified: 1147408274473
Doget: 1147408274473
This indicates that after cacheservlet calls the getlastmodified method, it then calls the doget method. Because the getlastmodified method returns the current time, it must be newer than the time value specified by the IF-modified-since request header field in the request message sent by the browser (that is, the "current time" returned when the getlastmodified method was called last time, after the cacheservlet calls the getlastmodified method, it then calls the doget method, so the new content is displayed in the browser.
(9) modify the cacheservlet. Java source file and let the last return statement in the getlastmodified method return a fixed time value, as shown below:
Protected long getlastmodified (httpservletrequest req)
{
Long Now = system. currenttimemillis ();
System. Out. println ("getlastmodified:" + now );
Return/* now */1234;
}
The "cache test" hyperlink on the hosts page accesses the cacheservlet. The displayed content of the cacheservlet page is still the last one in step (8). It has not changed! View the command line window for starting tomcat. Only a line of new information similar to the following is displayed:
Getlastmodified: 1147414361586
This phenomenon indicates that the browser actually sent an access request to the server. Because the request message sent by the Browser contains an IF-modified-since request header field, the value is set to the time specified by the last-modified header field, the time value returned by the getlastmodified method in cacheservlet is older than the time value specified by the last-modified header field (in general, it should be equal. Here, to simplify programming, only returns the old time value 1234, but does not affect the experiment effect, cacheservlet does not call the doget method (the prompt message for the doget method to be called is not printed in the Tomcat command line window), but returns a 304 (not modified) to the browser) the Status Code indicates that the version cached by the browser is the latest, so that the browser still displays the content cached last time.
Refresh the access to cacheservlet in the browser window. The content displayed in the browser remains unchanged. Check the command line window for starting tomcat, you can see that only a prompt message indicating that the getlastmodified method is called is printed. It can be seen that if the response message of a page cached by the Browser contains the last-modified header field, when the browser sends an access request to the server again, as long as the time value returned by the getlastmodified method of the servlet program that processes this page is earlier than the time value of the last-modified header field or is the same as the time value, the server will not return new content.
(10) Repeat the Telnet access operation in step (7). After the server returns the response result, enter the following content:
GET/it315/servlet/cacheservlet HTTP/1.1 <press enter>
HOST: <space> <press enter>
If-modified-since: <space> Thu, 01 Jan 1970 00:00:01 GMT
<Press enter>
The value in the IF-modified-since header is copied from the last-modified header field returned. At this time, we can see that the server returns only one 304 (not modified) status Code, but no entity content is returned. Enter the following content:
GET/it315/servlet/cacheservlet HTTP/1.1 <press enter>
HOST: <space> <press enter>
If-modified-since: <space> Thu, 01 Jan 1970 00:00:00 GMT
<Press enter>
Note: The value in the IF-modified-since header field is 1 second lower than the previous value. At this time, the server returns the 200 Status Code and the new object content, as shown in Figure 4.22.


Fig 4.22

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.