Research on cache in Web Development (I)
Research on cache in Web Development (2)
Research on cache in Web Development (III)
(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
Research on cache in Web Development (I)
Research on cache in Web Development (2)
Research on cache in Web Development (III)