Servlet进阶3

来源:互联网 发布:凌哥刷枪软件 编辑:程序博客网 时间:2024/06/06 04:46

http:要求:掌握一些头信息
 超文本传输协议:
  规定数据的格式
 浏览器往服务器发送 ---- 请求
 服务器往浏览器回写 ---- 响应

请求:(request)
 组成部分:
  请求行 请求头 请求体
  
 请求行:请求信息的第一行
  格式:请求方式 访问的资源 协议/版本
  例如:GET /day0801/1.html HTTP/1.1
  请求方式:get和post
   get会把参数放在url的后面 post不会
   get参数大小有限制,post请求却没有限制
   get请求没有请求体;post请求有请求体 请求参数放在请求体中
 请求头:请求信息的第二行到空行结束
  格式:key/value (value可以是多个值)
  常见的请求头:
   Accept: text/html,image/bmp  --支持数据类型    text/html text/css text/javascript 大类型/小类型 mime类型
   Accept-Charset: ISO-8859-1 --字符集
   Accept-Encoding: gzip  --支持压缩
   Accept-Language:zh-cn   --语言环境
   Host: www.click369.cn:80  --访问主机
   If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT   --缓存文件的最后修改时间
   Referer: http://www.click369.com/index.jsp  --来自哪个页面、防盗链
   User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
   Cookie
   Connection:Keep-Alive    --链接状态
   
  掌握的头信息:
   Referer User-Agent Cookie If-Modified-Since
 请求体:空行以下的内容
  只有post才有请求体  get请求参数 http://xxxx?username=tom&password=123
  格式:username=tom&password=123


响应:(response)
 组成部分:
  响应行 响应头 响应体
 响应行:响应信息的第一行
  格式:协议/版本 状态码 状态码说明
  例如:HTTP/1.1 200 OK
  状态码:
   200 正常响应成功
   302 重定向
   304 读缓存
   404 用户操作资源不存在
   500 服务器内部异常
 响应头:从响应信息的第二行到空行结束
  格式:key/value(value可以是多个值)
  常见的头
   Location: http://www.it315.org/index.jsp  --跳转方向 和302一起使用的
   Server:apache tomcat   --服务器型号
   Content-Encoding: gzip    --数据压缩
   Content-Length: 80    --数据长度
   Content-Language: zh-cn   --语言环境
   Content-Type: text/html; charset=GB2312   --数据类型
   Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT --最后修改时间
   Refresh: 1;url=http://www.it315.org  --定时刷新
   Content-Disposition: attachment; filename=aaa.zip --下载
   Set-Cookie:SS=Q0=5Lb_nQ; path=/search
   Expires: -1     --缓存
   Cache-Control: no-cache     --缓存
   Pragma: no-cache       --缓存
   Connection: Keep-Alive      --连接
  掌握的头信息
   Content-Type Location  Last-Modified Refresh Content-Disposition Set-Cookie
 响应体:空行以下的内容


 响应本地文件到桌面上


String realPath4 = context.getRealPath("/WEB-INF/classes/a.pdf");
//  File file = new File(realPath4);
  String mimeType = this.getServletContext().getMimeType(realPath4);//
  System.out.println(realPath4);
       System.out.println(mimeType);// 文件类型  application/pdf
  FileInputStream fis = new FileInputStream(new File(realPath4)); 
//  PrintWriter printWriter = response.getWriter();
  ServletOutputStream fos = response.getOutputStream();   //这里是Pdf格式,用字节流
  byte[] by = new byte[1024];
  int len = 0;
  while ((len = fis.read(by)) != -1) {
   String s = new String(by, 0, len);
//   fos.write(by, 0, len);
//   printWriter.write(s);
   fos.write(by, 0, len);
//   System.out.println(s);
  }
   fis.close();
   fos.close();