Servlet and cache (4), servlet Cache
1. Set two cache conditions:
First, you can set a reasonable Cache Time Value for infrequently changed data in the servlet to avoid frequent requests sent by the browser to the server and improve the server performance.
Type 2: If you want to implement an advanced function, that is, when the client requests dynamic web resources, the dynamic web resources will send the latest data to the client if the data sent to the client is updated, if the data is not updated, the dynamic web resource requires the client to access its own cached data. In this case, you can override the getLastModify Method of Dynamic web Resources (servlet.
The getLastModified method is called by the service method. By default, the getLastModified method returns a negative number. When a developer writes a servlet, if the getLastModified method is not overwritten, each time the servlet is accessed, the service method finds that the getLastModified method returns a negative number, and it will call the doXXX method to return the latest data to the client. In this case, when the server returns the data returned by the doXXX Method to the client, the Last-Modified header field is not added to the data.
2. ideas and methods:
If the getLastModified method is overwritten during servlet writing and a time value is returned, when the client accesses the Servlet, the service method first checks whether the client has a time value in the If-Modified-Since header field. If not, the service method calls the doXXX method to return the latest data to the client. When the data is returned, the service method also calls the getLastModified method to obtain a Time Value and adds the Last-Modified header field to the data. (Notify the client to cache data)
When the client accesses the servlet, If a time value is included in the If-Modified-Since header field, the service method first calls the getLastModified method before calling the doXXX method, obtain a Time Value and compare it with the time value brought by the client. If it is newer than the time value of the client, the service method calls the doXXX method to return the latest data to the client. If it is old, the service method will not call the doXXX method to return data to the client, but will return a 304 status code to the client to notify the client to get the data cached in it.
3. cached applications
A website has many static resources, such as css files, html pages, and GIF images. Once these files are created, they may never be updated. When the client accesses these files for the first time, the server should notify the client to cache these files while handing over the file data to the client. If the server finds that the files are not updated, the client should take the files cached by the client to relieve the pressure on the server.
All static web Resources on the Tomcat server are read and sent back to the client by a default servlet, which is used to improve the server performance.
4. Small cache code exercises
Package cn. wwh. www. web. servlet;
Import java. io. File;
Import java. io. IOException;
Import java. io. InputStream;
Import javax. servlet. ServletContext;
Import javax. servlet. ServletException;
Import javax. servlet. ServletOutputStream;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
/**
* Category: the cache application is simply practiced and overwritten.
*
* @ Author
* @ Version 1.0
* @ Creation Time: 02:28:50
*/
Public class CacheDate extends HttpServlet {
// Override the getLastModified Method
@ Override
Protected long getLastModified (HttpServletRequest req ){
// Returns the string containing the real path of the given virtual path.
String filePath = this. getServletContext (). getRealPath ("/login.html ");
System. out. println ("actual file path:" + filePath );
File file = new File (filePath );
// Return the last modification time of the file indicated by this abstract path name.
Return file. lastModified ();
}
Public void doGet (HttpServletRequest request,
HttpServletResponse response)
Throws ServletException, IOException {
ServletContext context = this. getServletContext ();
InputStream is = context. getResourceAsStream ("/login.html ");
ServletOutputStream sos = response. getOutputStream ();
Int len = 0;
Byte [] buf = new byte [1, 1024];
While (len = is. read (buf)> 0 ){
Sos. write (buf, 0, len );
}
Sos. flush ();
Sos. close ();
}
}
5. Conclusion:
(1) For browser refresh, when there are resources in the browser cache, you will also find resources on the server side. You can find the cache only when you press Enter. This can be observed through httpwatch.
(2) You can override the getLastModified () method to prevent access to the server for each refresh, so as to relieve pressure on the server.
(3) set the cache time by sending the packet header
Long end = Long. parseLong (date) * 1000 + System. currentTimeMillis ();
Response. setDateHeader ("expires", end); // The cache retention time sent to the client browser
What is the cache-free setting in servlet?
Response. setContentType ("text/xml"); // display html text
Response. setCharacterEncoding ("UTF-8"); // set the response Character Set
Response. setHeader ("Cache-Control", "no-cache, must-revalidate"); // you can specify no Cache.
How to solve the Servlet cache problem?
What happened to the cache?
Output cache can be used:
${CachedFragment1 }$ {counter }$ {cachedFragment2}