Location: http://www.baidu.org/index.jsp 【让浏览器重新定位到url】
Server:apache tomcat 【告诉浏览器我是tomcat】
Content-Encoding: gzip 【告诉浏览器我使用 gzip】
Content-Length: 80 【告诉浏览器会送的数据大小80节】
Content-Language: zh-cn 【支持中文】
Content-Type: text/html; charset=GB2312 [内容格式text/html; 编码gab2312]
Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT 【告诉浏览器,该资源上次更新时间】
Refresh: 1;url=http://www.baidu.com 【过多久去,刷新到 http://www.baidu.com】
Content-Disposition: attachment; filename=aaa.zip 【告诉浏览器,有文件下载】
Transfer-Encoding: chunked [传输的编码]
Set-Cookie:SS=Q0=5Lb_nQ; path=/search[后面详讲]
Expires: -1[告诉浏览器如何缓存页面IE]
Cache-Control: no-cache [告诉浏览器如何缓存页面火狐]
Pragma: no-cache [告诉浏览器如何缓存页面]
Connection: close/Keep-Alive [保持连接 1.1是Keep-Alive]
Date: Tue, 11 Jul 2000 18:23:51 GMT
①定时刷新Refresh使用
response.setHeader(“Refresh”, “5;url=/servletPro/Servlet2”);
②文件下载 Content-Disposition
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
//PrintWriter out = response.getWriter();
//演示下载文件
response.setHeader("Content-Disposition", "attachment; filename=winter.jpg");
//打开文件.说明一下web 站点下载文件的原理
//1.获取到要下载文件的全路径
String path=this.getServletContext().getRealPath("/images/Winter.jpg");
//System.out.println("path="+path);
//2创建文件输入流
FileInputStream fis=new FileInputStream(path);
//做一个缓冲字节数组
byte buff[]=new byte[1024];
int len=0;//表示实际每次读取了多个个字节
OutputStream os=response.getOutputStream();
while((len=fis.read(buff))>0){
os.write(buff, 0, len);
}
//缺点: 没有进度条./图标/
//关闭
os.close();
fis.close();
}
③缓存讲解
提出问题:浏览器默认情况下,会缓存我们的页面,这样出现一个问题:如果我们的用户习惯把光标停留在地址栏,然后回车来取页面,就会默认调用cache中取数据。
(1) 有些网站要求及时性很高,因此要求我们不缓存页面
代码:
//指定该页面不缓存 Ie
response.setDateHeader(“Expires”, -1);【针对IE浏览器设置不缓存】
//为了保证兼容性.
response.setHeader(“Cache-Control”, “no-cache”);【针对火狐浏览器等】
response.setHeader(“Pragma”, “no-cache”);【其他浏览器】
(2) 有些网站要求网页缓存一定时间,比如缓存一个小时
response.setDateHeader(“Expires”, System.currentTimeMillis()+3600100024);后面一个参数表示设置的缓存保持时间,-1表示永远缓存