I. Common response headers in HTTP
- location:http://www.it315.org/index.jsp
- Server:apache Tomcat
- Content-encoding:gzip
- Content-length:80
- Content-language:zh-cn
- content-type:text/html; charset=gb2312
- Last-modified:tue, Jul 18:23:51 GMT
- refresh:1;url=http://www.it315.org
- Content-disposition:attachment; Filename=aaa.zip
- Transfer-encoding:chunked
- SET-COOKIE:SS=Q0=5LB_NQ; Path=/search
- etag:w/"7777-1242234904000"
- Expires:-1
- Cache-control:no-cache
- Pragma:no-cache
- Connection:close/keep-alive
- Date:tue, Jul 18:23:51 GMT
Second, set the cache information
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
System.out.println ("-----------cacheservlet-------------");
Set the appropriate header information
Set cache Time 100 seconds
Response.setdateheader ("Expires",
System.currenttimemillis () +100*1000);
Disable the use of caching
Response.setdateheader ("Expires", 0);
Response.setheader ("Cache-control", "No-cache");
Response.setheader ("Pragma", "No-cache");
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ");
Out.println ("<HTML>");
Out.println ("Out.println ("<BODY>");
Read file
String path = Getservletcontext (). Getrealpath ("/a.txt");
FileReader reader = new FileReader (path);
Char buffer[] = new char[256];
int len = 0;
while (len = reader.read (buffer))! =-1) {
Out.println (new String (buffer, 0, Len));
}
Reader.close ();
Out.println ("</BODY>");
Out.println ("</HTML>");
Out.flush ();
Out.close ();
}
/**
* Time of last modification
*/
@Override
Protected long getlastmodified (HttpServletRequest req) {
String path = Getservletcontext (). Getrealpath ("/a.txt");
File File = new file (path);
return file.lastmodified ();
}
A.txt File Contents:
A.txt placement address in the project:
Results:
Third, the download function source code is as follows
public class Downservlet extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
String path = Request.getservletcontext (). Getrealpath ("/down/China. png");
File File = new file (path);
Download the way to open this operation (Specify the encoding method, the download file name is consistent with the source file)
Response.AddHeader ("Content-disposition", "Attachment;filename="
+ Urlencoder.encode (File.getname (), "UTF-8"));
OutputStream OS = Response.getoutputstream ();
InputStream is = new FileInputStream (file);
int len = 0;
byte[] buffer = new byte[1024];
while (len = is.read (buffer))! =-1) {
Os.write (buffer, 0, Len);
}
Is.close ();
Os.close ();
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
}
The address of the downloaded file in this program is placed in the following location of the project:
Javaweb Learning Record (i)--response response header cache setting and download function implementation