web和http协议分析(web服务器与http协议重点学习)
Web服务器
l WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源。
l Internet上供外界访问的Web资源分为:
• 静态web资源(如html 页面):指web页面中供人们浏览的数据始终是不变。
• 动态web资源:指web页面中供人们浏览的数据是由程序产生的,不同时间点访问web页面看到的内容各不相同。
l 静态web资源开发技术
• Html
l 常用动态web资源开发技术:
• JSP/Servlet、ASP、PHP等 ruby python
1. 将webproject部署到tomcat中
6.HTTP协议
HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的过程。
HTTP协议是学习JavaWEB开发的基石,不深入了解HTTP协议,就不能说掌握了WEB开发,更无法管理和维护一些复杂的WEB站点。
示例1
telnet怎样使用
1.telnet localhost 8080
2 ctrl ]
3.按回车
注意 在里面写错的内容不能修改
GET /index.html HTTP/1.1
host:localhost
4.要敲两次回车
HTTP/1.0版本只能保持一次会话
HTTP/1.1版本可能保持多次会话.
是根据telnet得到的响应信息
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
ETag: W/"7347-1184876416000"
Last-Modified: Thu, 19 Jul 2007 20:20:16 GMT
Content-Type: text/html
Content-Length: 7347
Date: Thu, 25 Apr 2013 08:06:53 GMT
Connection: close
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Apache Tomcat</title>
<style type="text/css">
..........
示例2
是根据httpwatch得到的请求信息与响应信息
请求
GET / HTTP/1.1
Accept: application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
Accept-Encoding: gzip, deflate
Host: localhost
Connection: Keep-Alive
响应
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
ETag: W/"7347-1184876416000"
Last-Modified: Thu, 19 Jul 2007 20:20:16 GMT
Content-Type: text/html
Content-Length: 7347
Date: Thu, 25 Apr 2013 08:12:57 GMT
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang
请求信息详解
GET /books/java.html HTTP/1.1 ---------->请求行
Get是请求方式 /books/java.html 请求资源 HTTp/1.1协议版本
POST与GET的区别
1.什么样是GET 请求 1)直接在地址栏输入 2.超连接 <a></a> 3.form表单中method=get
什么样是POSt请求 form表单中method=POST
2.以get方式提交请求时,在请求行中会将提交信息直接带过去
格式 /day03_1/login?username=tom&password=123
以post方式提交时,信息会在正文中。
POST /day03_1/login HTTP/1.1
Accept: application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://localhost/day03_1/login.html
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost
Content-Length: 25
Connection: Keep-Alive
Cache-Control: no-cache
username=tom&password=123
3. get方式最多能提交1kb
post可以提交大数据,做上传时必须是post
Accept: */* 允许访问mime类型,类型都在tomcat 的conf/web.xml文件中定义了。
这个需要知道,因为做下载时要知道mime类型
Accept-Language: en-us 客户端的语言
Connection: Keep-Alive 持续连接
Host: localhost 客户端访问资源
Referer: http://localhost/links.asp (重点) 防盗链。
User-Agent: Mozilla/4.0 得到浏览器版本 避免兼容问题
Accept-Charset: ISO-8859-1 客户端字符编码集
Accept-Encoding: gzip, deflate gzip是压缩编码.
If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT 与Last-MOdified一起可以控制缓存。
Date: Tue, 11 Jul 2000 18:23:51 GMT
示例1
防盗链程序
referer.htm页面
<body>
<a href="referer">referer</a>
</body>
RefererServlet类似
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String msg = request.getHeader("Referer");
if (msg != null && "http://localhost/day03_1/referer.html".equals(msg)) {
// 如果你是正常访问,我们给其一个友好信息
response.getWriter().write("hello");
} else {
// 如果是盗链过来的,对不。骂它一句
response.getWriter().write("fuck...");
}
}
怎样破解
URL url = new URL("http://localhost/day03_1/referer"); //得到一个url
URLConnection con = url.openConnection(); //访问这个url,并获得连接对象
con.addRequestProperty("Referer",
"http://localhost/day03_1/referer.html");
InputStream is = con.getInputStream(); // 读取服务器返回的信息.
byte[] b = new byte[1024];
int len = is.read(b);
System.out.println(new String(b, 0, len));
http协议响应
HTTP/1.1 200 OK 响应状态行
HTTP/1.1 200 OK
1xx 什么都没做直接返回
2xx 成功返回
3xx 做了一些事情,没有全部完成。
4xx 客户端错误
5xx 服务器错误
200 正确
302 重定向
304 页面没有改变
404 未找到页面
500 服务器出错.
Location: http://www.it315.org/index.jsp 响应路径(重点) 302
Server:apache tomcat
Content-Encoding: gzip 响应编码 gzip 压缩
Content-Length: 80 响应长度
Content-Language: zh-cn 响应语言
Content-Type: text/html; charset=GB2312 响应字符编码
Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT 要与请求中的 If-Modified-Since处理缓存
Refresh: 1;url=http://www.it315.org 自动跳转
Content-Disposition: attachment; filename=aaa.zip (重要) 文件的下载
//下面三个是禁用浏览缓存
Expires: -1
Cache-Control: no-cache
Pragma: no-cache
Connection: close/Keep-Alive
Date: Tue, 11 Jul 2000 18:23:51 GMT
重点
今天可以讲
Location: http://www.it315.org/index.jsp 响应路径(重点) 302
Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT 要与请求中的 If-Modified-Since处理缓存
Refresh: 1;url=http://www.it315.org 自动跳转
我们在得到响应信息,经常得到的是压缩后的。
这种操作
1.服务器配置方式
tomcat配置实现压缩
80端口没有配置 00:00:00.000 0.228 7553 GET 200 text/html http://localhost/
8080端口配置 00:00:00.000 0.027 2715 GET 200 text/html http://localhost:8080/
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" compressableMimeType="text/html,text/xml,text/plain" compression="on"/>
2.通过我们编程实现.(后面会讲)
后面会讲
Content-Disposition: attachment; filename=aaa.zip (重要) 文件的下载
//下面三个是禁用浏览缓存
Expires: -1
Cache-Control: no-cache
Pragma: no-cache
4.启动服务器
5.在浏览器中访问web资源.
获取更多资源关注【Java帮帮】微信公众号与QQ空间
,免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com