http协议内容
请求(浏览器-》服务器) Request
GET /day09/hello HTTP/1.1--请求行
Host: localhost:8080---请求头(多个key-value对象)
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
---一个空行
name=husky&password=adios ----(可选)实体内容(只有post提交的参数会放到实体内容中))
响应(服务器-》浏览器)Response
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Length: 24
Date: Fri, 30 Jan 2015 01:54:57 GMT
this is hello servlet!!!
http协议版本
http1.0: 当前浏览器客户端与服务器建立连接后,只能发送一次请求,
一次请求之后连接关闭
http1.1:当前浏览器客户端和服务器建立连接之后,可以在一次连接中发送多次请求。(基本上都使用1.1版本)
请求方式
常见请求方式:GET和POST
1)GET方式提交
a)地址栏(url)会跟上参数数据。以?开头,多个参数以&分割
GET /day09/testMethod.html?name=eric&password=123456 HTTP/1.1
b)GET提交参数数据有限制,不超过1kb
c)GET方式不适合提交敏感密码。
d)注意:浏览器直接访问的请求,默认提交方式是GET方式。
2)POST方式提交
a)参数不会跟在URI后面。参数而是跟在请求的实体内容中。没有?开头,而是以&连接
name=eric&password=123456
b)post提交的参数数据没有限制。
c)post方式提交铭感数据
请求头
Accept: text/html,image/* -- 浏览器接受的数据类型
Accept-Charset: ISO-8859-1 -- 浏览器接受的编码格式
Accept-Encoding: gzip,compress --浏览器接受的数据压缩格式
Accept-Language: en-us,zh- --浏览器接受的语言
Host: www.it315.org:80 --(必须的)当前请求访问的目标地址(主机:端口)
If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT --浏览器最后的缓存时间
Referer: http://www.it315.org/index.jsp -- 当前请求来自于哪里
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) --浏览器类型
Cookie:name=eric -- 浏览器保存的cookie信息
Connection: close/Keep-Alive -- 浏览器跟服务器连接状态。close: 连接关闭 keep-alive:保存连接。
Date: Tue, 11 Jul 2000 18:23:51 GMT -- 请求发出的时间
通过servlet获得浏览器发过来的请求数据
RequestDemo2.java
package com.husky.javaweb;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
@WebServlet(name = "RequestDemo2",urlPatterns = "/RequestDemo2")
public class RequestDemo2 extends HttpServlet {
//为了接收POST方式提交的请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
t3(request);
}
/**
* 1)tomcat服务器接受到浏览器发送的请求数据,然后封装到HttpServletRequest对象中
* 2)tomcat服务器调用doGet方法,然后把request对象传入到servlet中。
*
* */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**
* 3)从request对象取出请求数据
* */
/**
* 3.1请求行 格式:(GET /hello http1.1)
* */
//t1(request);
/**
* 3.2请求头
* */
//t2(request);
/**
* 3.3请求的实体内容
* 在doPost中实现
* */
}
private void t3(HttpServletRequest request) throws IOException {
InputStream in = request.getInputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len = in.read(buf))!= -1){
String str = new String(buf,0,len);
System.out.println(str);
}
}
private void t2(HttpServletRequest request) {
String host = request.getHeader("Host");
System.out.println(host);
//变量
Enumeration<String> enums = request.getHeaderNames();
while(enums.hasMoreElements()){
String headerName = enums.nextElement();
String headerValue = request.getHeader(headerName);
System.out.println(headerName + " : "+headerValue);
}
}
private void t1(HttpServletRequest request) {
System.out.println("请求方式: "+request.getMethod());
System.out.println("URI: "+request.getRequestURI());
System.out.println("URL: "+request.getRequestURL());
System.out.println("http协议版本:"+request.getProtocol());
}
}