[关闭]
@duyao 2015-05-01T01:02:13.000000Z 字数 4248 阅读 1649

http协议

servlet


http协议

是建立在tip/ip协议基础上的,全称是超文本传输协议,有1.0和1.1版本,目前通常使用1.1版本
http1.0是短连接,http1.1是长连接,这里的长和短是指持续时间

http的请求

请求行
多个消息头,格式是消息名:格式
空行
发送的内容

每次消息头都不一定相同

  1. GET http://localhost:8080/userManager/test.html[请求行]
  2. Host: localhost:8080[消息头]
  3. User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0
  4. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  5. Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
  6. Accept-Encoding: gzip, deflate
  7. Connection: keep-alive
  8. If-Modified-Since: Mon, 20 Apr 2015 07:43:07 GMT
  9. If-None-Match: W/"528-1429515787026"
  10. Cache-Control: max-age=0
  11. [这是一个空行]
  12. [无发送内容]
请求行
GET http://localhost:8080/userManager/test.html
请求方式:包括get和post 请求内容
常见的消息头
http的响应

状态行
多个响应头,格式是消息名:格式
空行
实体内容

  1. HTTP/1.1 200 OK[状态行]
  2. Server: Apache-Coyote/1.1
  3. Accept-Ranges: bytes
  4. ETag: W/"515-1429577425144"
  5. Last-Modified: Tue, 21 Apr 2015 00:50:25 GMT
  6. Content-Type: text/html
  7. Content-Length: 515
  8. Date: Tue, 21 Apr 2015 01:43:59 GMT
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  10. <html>
  11. <head>
  12. <title>test.html</title>
  13. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  14. <meta http-equiv="description" content="this is my page">
  15. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  16. <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  17. </head>
  18. <body>
  19. <h1>hello</h1>
  20. <img src="1.jpg" />
  21. <img src="2.jpg" />
  22. </body>
  23. </html>
状态行
HTTP/1.1 200 OK
http版本 状态码 信息

状态码
200:整个响应和请求没有发生错误
302:表示当你请求一个资源时,服务器返回302表示让浏览器转向到另外一个资源,比如,responce.sendRedirect("url")
404:找不到资源
500:服务器端出现错误

常见的响应头
  1. package com.dy.http;
  2. import java.io.FileInputStream;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.io.PrintWriter;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. public class Download extends HttpServlet {
  12. /**
  13. * 演示页面下载功能,使用Content-Disposition关键字
  14. */
  15. public void doGet(HttpServletRequest request, HttpServletResponse response)
  16. throws ServletException, IOException {
  17. response.setContentType("text/html;charset=utf-8");
  18. //getWriter和getOutputStream不能同时出现
  19. //PrintWriter out = response.getWriter();
  20. //下载文件的报头
  21. response.setHeader("Content-Disposition", "attachment;filename=1.jpg");
  22. //1.获取全路径
  23. String path=this.getServletContext().getRealPath("/image/1.jpg");
  24. //System.out.println(path);
  25. //2.创建文件输入流,这样才可以下载
  26. FileInputStream fis=new FileInputStream(path);
  27. //创建缓冲
  28. byte [] buffer=new byte[1024];
  29. //每次实际读出多少比特
  30. int len=0;
  31. OutputStream os=response.getOutputStream();
  32. while((len=fis.read(buffer))>0){
  33. os.write(buffer, 0, len);
  34. }
  35. //关闭流
  36. os.close();
  37. fis.close();
  38. }
  39. public void doPost(HttpServletRequest request, HttpServletResponse response)
  40. throws ServletException, IOException {
  41. this.doGet(request, response);
  42. }
  43. }
  1. responce.setDataHeader("Expires",-1);
  2. response.setHeader("Cache-Control","no-cache");
  3. response.setHeader("Pragma","no-cache");
  4. //缓存一定时间,缓存一小时
  5. responce.setDataHeader("Expires",System.currentTimeMillis()+3600*1000);
http的通用信息头
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注