您的位置:首页 > 理论基础 > 计算机网络

http协议介绍(servlet)

2015-12-26 12:10 801 查看

http://www.androidchina.net/3934.html

1.什么是http

_ 1.http协议:_

1. 复杂解释:

http(超文本传输协议)是一个基于请求与响应模式的、无状态的、应用层的协议,常基于TCP的连接方式,HTTP1.1版本中给出一种持续连接的机制,绝大多数的Web开发,都是构建在HTTP协议之上的Web应用。

2. 简单说:

对浏览器客户端 和 服务器端 之间数据传输的格式规范.

3. 协议版本:

http1.0:当前浏览器客户端与服务器端建立连接之后,只能发送一次请求,一次请求之后连接关闭。

http1.1:当前浏览器客户端与服务器端建立连接之后,可以在一次连接中发送多次请求。(基本都使用1.1)

请求一次资源就会出现一次请求,比如三张图片,就有三次请求,如果图片是一样 的就只有一次请求;

2.查看http协议的工具:

1. chrome(谷歌)浏览器查看:

右键点击查看元素(inspect element),点击network即可;


2. 火狐:

使用火狐的firebug插件(右键->firebug->网络)

3. 使用系统自带的telnet工具(远程访问工具)

a)telnet localhost 8080 访问tomcat服务器

b)ctrl+] 回车.可以看到回显

c)输入请求内容,回车,即可查看到服务器响应信息。
GET / HTTP/1.1
Host: www.baidu.com


2.http协议内容:

请求:
GET /HttpSer HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.85 Chrome/45.0.2454.85 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8


响应:
HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
Location: http://localhost:8080/HttpSer/ Transfer-Encoding: chunked
Date: Fri, 09 Oct 2015 08:55:42 GMT


下面将对这两个协议进行介绍:


3.http请求介绍:

GET /HttpSer HTTP/1.1-请求行
Host: localhost:8080--请求头;有多个key-value组成
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.85 Chrome/45.0.2454.85 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
--可选,实体内容;


请求行介绍

GET /HttpSer HTTP/1.1 -请求行

1.请求资源的URL和URI比较:

URL: 统一资源定位符。http://localhost:8080/HttpSer/index.html。只能定位互联网资源。是URI 的子集.

URI: 统一资源标记符。/HttpSer/index.html。用于标记任何资源。可以是本地文件系统,局域网的资源(//192.168.14.10/HttpSer/index.html),可以是互联网。

2.请求方式:

常见的请求方式: GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETE

常用的请求方式: GET(有将实体信息放在浏览器地址栏) 和 POST(隐藏实体内容)

3. servlet获得请求行信息:_
/*** 1.1请求行的获得*/
System.out.println("请求方式:"+request.getMethod());//获得提交方式
System.out.println("请求URI:"+request.getRequestURI());//获得uri
System.out.println("请求url:"+request.getRequestURL());//获得url
System.out.println("获得协议:"+request.getProtocol());//获得所用协议
##输出:
请求方式:GET
请求URI:/HttpSer/TestRequst
请求url:http://localhost:8080/HttpSer/TestRequst
获得协议:HTTP/1.1


4. 测试提交方式:

新建立web工程,建立TestMethod.html文件:



建立Servlet类TestMethod.java修改get和post方法:

运行tomcat可以看淡提交方式的不同:浏览器地址栏显示的不同,servlet调用的方法也不同;

get提交



post提交



经过对比请求可以发现

get方式在请求行多了?name=wang&pass=123

post多了实体内容:Content-Type: application/x-www-form-urlencoded

请求内容同如下:
#get方式:
GET /HttpSer/TestMethod?name=wang&pass=123 HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.85 Chrome/45.0.2454.85 Safari/537.36
Referer: http://localhost:8080/HttpSer/testMethod.html Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
#post方式:
POST /HttpSer/TestMethod HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 18
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://localhost:8080 Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.85 Chrome/45.0.2454.85 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost:8080/HttpSer/testMethod.html Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8


请求头介绍:

1. 请求头介绍:

请求头主要包含一些有用信息:

1.Host: localhost:8080主机地址和端口

2.Connection: keep-alive 连接方式

3.User-Agent:浏览器的一些信息

4.Referer:来访页面

5.Content:实体内容;post才有

2. servlet获得请求头主要的方法:

request.getHeader(“Host”));通过建获得相应请求的内容;

Enumeration headerNames = request.getHeaderNames();获得请求头所有的键值

3. 演示如下:

修改doget方法

#输出如下:
请求方式:GET
请求URI:/HttpSer/TestRequst
请求url:http://localhost:8080/HttpSer/TestRequst
获得协议:HTTP/1.1
获得host:localhost:8080
获得浏览器的User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.101 Chrome/45.0.2454.101 Safari/537.36
host:localhost:8080
connection:keep-alive
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
upgrade-insecure-requests:1
user-agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.101 Chrome/45.0.2454.101 Safari/537.36
referer:http://localhost:8080/HttpSer/testMethod.html
accept-encoding:gzip, deflate, sdch
accept-language:en-US,en;q=0.8
cookie:CNZZDATA1255712369=1133597550-1443969628-%7C1443969628//此去后面文章会介绍;


输入参数的介绍:

1. 输入参数:

输入参数:

对于get来说就是跟在url后面的内容

/TestParam?name=”peace”&password=”sisi” ;name=”peace”&password=”sisi”这就是输入参数

对于post来说就是实体内容,不可见

2. Servlet中获得输入参数的方法:

String name=request.getParameter(“name”);获得对应输入参数名字的内容

Enumeration params = request.getParameterNames();获得所有输入参数的名字,返回一个迭代器

String[] hobits = request.getParameterValues(names);如果对应名字的内容是一个数组,使用这个方法获得,比如复选框

3. 演示如下:

1.建立testParam.html



2.修改doget方法:

##输出结果如下:
提交方式:POST
name:peace,pass:124
>>>>>>>>>>>>>>>>>
name:peace
password:124
gender:男
籍贯:湖南
hobit:
篮球,足球,
info:一条小鲨鱼peace
id:001


4.http响应介绍:

HTTP/1.1 302 Found  ---响应行
Server: Apache-Coyote/1.1 ---响应头, 有多个key-value组成
Location: http://localhost:8080/HttpSer/ Transfer-Encoding: chunked
Date: Fri, 09 Oct 2015 08:55:42 GMT


响应行介绍:

HTTP/1.1 302 Found

1基本介绍

1.HTTP/1.1:采用的协议

2.302:状态码

常见的状态:

200 : 表示请求处理完成并完美返回

302: 表示请求需要进一步细化。

404: 表示客户访问的资源找不到。

500: 表示服务器的资源发送错误。(服务器内部错误)

3.Found:状态描述,常见ok和found

2. servlet中的方法

tomcat服务器把请求信息封装到HttpServletRequest对象,且把响应信息封装到HttpServletResponse

response.setStatus(404);//设置状态码

response.sendError(404);// 设置错误页面

3. 演示如下
response.setStatus(404);//错误代码,没有反应
response.sendError(404);// 发送404的状态码+404的错误页面
#输出结果:
HTTP/1.1 404 Not Found
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=ISO-8859-1
Content-Language: en
Content-Length: 949
Date: Sat, 10 Oct 2015 13:09:53 GMT





响应头介绍:

1. 基本介绍

格式:Server: Apache-Coyote/1.1;Server响应头名,后面的是响应值;

头里面主要包括:Server,服务器类型;Location:跳转网页地址 Conten*:实体内容

2. servlet中的方法

response.setHeader(“server”, “JBoss”);修改对应头名的内容;

3. 演示如下

#输出如下:
HTTP/1.1 200 OK
server: JBoss
Content-Length: 12
Date: Sat, 10 Oct 2015 13:11:04 GMTHTTP/1.1 200 OK
server: JBoss
Content-Length: 12
Date: Sat, 10 Oct 2015 13:11:04 GMT





几个常要的方面介绍

测试重定向:与转发不同

#输出:
HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
location: /HttpSer/adv.html
Content-Length: 12
Date: Sat, 10 Oct 2015 13:15:26 GMT


定时刷新:

#输出:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
refresh: 1
Content-Length: 12
Date: Sat, 10 Oct 2015 13:18:39 GMT
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
refresh: 5;url=/HttpSer/adv.html
Content-Length: 12
Date: Sat, 10 Oct 2015 13:21:29 GMT


设置编码:

设置为下载方式打开文件:

发送硬盘图片给浏览器:

来自一条小鲨鱼(rlovep.com)

代码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: