1. Set the effective date of the cached File
Key Methods:
Httpservletrequest class:
1 "string getrequesturi (); // obtain the PATH value in the client Address Bar
Httpservletresponse class:
2 void setdateheader (string name, long endtime );
Name: the browser type. Internet Explorer: expires
Endtime: the time when the cached file expires, in milliseconds.
Code:
String uri = request. getrequesturi ();
// If it is an HTML static resource
If (Uri! = NULL & URI. endswith ("html ")){
String html = This. getservletconfig (). getinitparameter ("html ");
If (HTML! = NULL ){
Long end = long. parselong (HTML) * 1000 + system. currenttimemillis ();
Response. setdateheader ("expires", end );
}
}
2. You can use the getlastmodified method to prevent access to the server every time you refresh it, to relieve pressure on the server.
Principle:
1. When a servlet object responds to a client request, it first calls the Service () method
2, view the source code of the service () method, you can know:
The Service () method calls the getlastmodified () method,
If the return value of this method> time of the Client: Call the doget () or dopost () method, and send the status code 200 to the client.
If the return value of this method = client time: Send status code 304 to the server
If the return value of this method is-1: Call the doget () or dopost () method and send the status code 200 to the client.
Method prototype:
Protected long getlastmodified (httpservletrequest req );
Return Value:
Return time: the last revision time of the httpservletrequest object,
If the time is unknown, this method returns a negative number (default)
1. The returned value is-1, indicating that the last modification time of the file is unknown.
Processing Method: because you do not know the last modification time, you must call the doget () or dopost () method to obtain resources again.
2. Return Value not equal to-1: indicates the last modification time of the file.
Processing Method: Compare the last modification time of the file with the cache time (this process is implemented through the Tomcat server)
If the file has been updated, call the doget () or dopost () method to obtain the resource again (and send: 202)
If the file version is the same, the 304 status code will be sent so that the client can find it in the cached file.
Ideas:
Therefore, we can use the lastmodified () method of the resource object to obtain the time when the server last modified the resource.
Supplement:
Servlet Source Code:
Servlet {sun source code
Service ()
{
If (req. gethead ("If-lastxxx") = NULL)
{
Res. sethead ("lastxx", "Time Value ");
Doxxx ();
}
If (getlastmodified ()> ie time value)
{
Res. sethead ("lastxx", "Time Value ");
Doxxx ();
} Else {
Res. setstatus (304 );
}
}
Getlastmodified (){
Return Time Value
}
}
Code:
/* Set */
/* This code has a problem */
Public class demo7 extends httpservlet {
@ Override
Protected long getlastmodified (httpservletrequest req ){
System. Out. println ("modifi ");
/* Convert the resource path from the project path to the actual path */
String Path = This. getservletcontext (). getrealpath ("/doc/resource.txt ");
File file = new file (PATH );
Return file. lastmodified ();
}
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Servletcontext context = This. getservletcontext ();
Inputstream is = context. getresourceasstream ("/doc/resource.txt ");
Servletoutputstream SOS = response. getoutputstream ();
Byte [] Buf = new byte [1, 1024];
Int Len =-1;
While (LEN = is. Read (BUF)> 0 ){
SOS. Write (BUF, 0, Len );
}
System. Out. println ("serv ");
Is. Close ();
SOS. Close ();
}
}
Note:
1. cache refresh
For browser refresh, when the browser cache contains this resource, it will also ask the server to re-request the resource;
When you press enter, you can directly find the cache, instead of the server.
2
In fact, the HTML static resources accessed by the browser are essentially handled by a default servlet and forwarded to the client.
Supplement: common methods for servlet objects
1. Get a servletconfig object.
Servletconfig getservletconfig ();
2. Get a servletcontext object
Servletcontext getservletcontext ();
Http://www.cnblogs.com/SkyGood/p/3970507.html
Servlet advanced application-Servlet and Cache