@duyao
2015-05-01T01:02:13.000000Z
字数 4248
阅读 1724
servlet
是建立在tip/ip协议基础上的,全称是超文本传输协议,有1.0和1.1版本,目前通常使用1.1版本
http1.0是短连接,http1.1是长连接,这里的长和短是指持续时间
请求行
多个消息头,格式是消息名:格式
空行
发送的内容
每次消息头都不一定相同
GET http://localhost:8080/userManager/test.html[请求行]
Host: localhost:8080[消息头]
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
If-Modified-Since: Mon, 20 Apr 2015 07:43:07 GMT
If-None-Match: W/"528-1429515787026"
Cache-Control: max-age=0
[这是一个空行]
[无发送内容]
GET | http://localhost:8080/userManager/test.html |
---|---|
请求方式:包括get和post | 请求内容 |
状态行
多个响应头,格式是消息名:格式
空行
实体内容
HTTP/1.1 200 OK[状态行]
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"515-1429577425144"
Last-Modified: Tue, 21 Apr 2015 00:50:25 GMT
Content-Type: text/html
Content-Length: 515
Date: Tue, 21 Apr 2015 01:43:59 GMT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>test.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<h1>hello</h1>
<img src="1.jpg" />
<img src="2.jpg" />
</body>
</html>
HTTP/1.1 | 200 | OK |
---|---|---|
http版本 | 状态码 | 信息 |
状态码
200:整个响应和请求没有发生错误
302:表示当你请求一个资源时,服务器返回302表示让浏览器转向到另外一个资源,比如,responce.sendRedirect("url")
404:找不到资源
500:服务器端出现错误
package com.dy.http;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Download extends HttpServlet {
/**
* 演示页面下载功能,使用Content-Disposition关键字
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//getWriter和getOutputStream不能同时出现
//PrintWriter out = response.getWriter();
//下载文件的报头
response.setHeader("Content-Disposition", "attachment;filename=1.jpg");
//1.获取全路径
String path=this.getServletContext().getRealPath("/image/1.jpg");
//System.out.println(path);
//2.创建文件输入流,这样才可以下载
FileInputStream fis=new FileInputStream(path);
//创建缓冲
byte [] buffer=new byte[1024];
//每次实际读出多少比特
int len=0;
OutputStream os=response.getOutputStream();
while((len=fis.read(buffer))>0){
os.write(buffer, 0, len);
}
//关闭流
os.close();
fis.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
responce.setDataHeader("Expires",-1);
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
//缓存一定时间,缓存一小时
responce.setDataHeader("Expires",System.currentTimeMillis()+3600*1000);