Tomcat与web程序结构与Http协议

telnet

一:打开telnet服务:
控制面板------> 程序和功能---> 打开或关闭windows功能---> 选中 Telnet客户端--->确定
二:测试telnet服务是否已经打开:


C:UsersAdministrator>telnet -?


telnet [-a][-e escape char][-f log file][-l user][-t term][host [port]]
 -a      企图自动登录。除了用当前已登陆的用户名以外,与 -l 选项相同。
 -e      跳过字符来进入 telnet 客户端提示。
 -f      客户端登录的文件名
 -l      指定远程系统上登录用的用户名称。
         要求远程系统支持 TELNET ENVIRON 选项。
 -t      指定终端类型。
         支持的终端类型仅是: vt100, vt52, ansi 和 vtnt。
 host    指定要连接的远程计算机的主机名或 IP 地址。
 port    指定端口号或服务名。


三:使用:
先把tomcat 打开
C:UsersAdministrator>telnet localhost 80
输入的字符看不到,按ctrl + ]即可
把下面两行代码粘进入,按两次enter后即可将apache主页打印出来.
GET / HTTP/1.1
Host:



将httpWathc professional启用

internet属性-->程序-->管理加载项-->将httpWathc professional启用

修改html编辑器



windows-->General-->Editors-->File Association
 File types:--->*.html
 Associated editors:-->MyEclispe HTML Editor (locked by 'html' content type)
 default ok.



访问http server的次数





例题:一个web页面中,使用img标签引用了三个图片,当客户端访问服务器中的这个web页面时,客户端
总共访问几次服务器.即向服务器发送了几次Http请求:
新建一个web project
在WebRoot中加入三个图片
在WebRoot中建立一个img.html文件,关键内容为:
<body>
<img  src="1.jpg">
<img  src="3.jpg">
<img  src="5.jpg">
</body>
将此工程发布出去,在IE浏览器中打开HttpWatch professional,Record
输入:http://localhost/myday02/img.html回车
结果:
 00:00:00.000 0.013 122 GET 304 text/html http://localhost/myday02/img.html
 00:00:00.017 0.007 125 GET 304 image/jpeg http://localhost/myday02/1.jpg
 00:00:00.018 0.014 125 GET 304 image/jpeg http://localhost/myday02/3.jpg
 00:00:00.018 0.039 125 GET 304 image/jpeg http://localhost/myday02/5.jpg
即有四次访问:一个html,三次图片.

Http请求



一:概述:客户端连上服务器后,向服务器请求某个web资源,称之为客户端向服务端发送了一个HTTP请求.一个
     完整的Http请求包括如下内容:
     一个请求行,若干个请求头,以及实体内容.
     请求行:请求行用于描述客户端的请求方式.请求的资源名称,以及使用的HTTP协议版本号
     多个请求行:消息头用于描述客户端请求哪台主机,以及客户端的一些环境信息等.
二:共有七种请求方式:POST,GET,HEAD,OPTIONS,DELETE,TRACE,PUT
    常用的有GET,POST,默认的是get请求.可通过更改表单的提交方式实现.


三:不管post还是get,都用于向服务器请求某个web资源,这两种方式的区别主要表现在数据传递上:
   1.get方式:可以在请求的url地址后以?的形式带上交给服务器的数据.多个数据之间以&进行分隔:
             如:
        1.1: get方式的特点:在URL地址后附带的参数是有限制的,其数据容量通常不能超过1K.
   2.POST方式:可以在请求的实体内容中向服务器发送数据,
        2.1: post方式的特点:传送的数据量无限制.
---------------------------------------------------------------------------------
示例演示区别:
在WebRoot中建立一个img.html文件,关键内容为:
<body>
<h2>get方式的请求</h2>
<form action="" method="get">
用户名:<input type = "text" name = "name">
密&nbsp;码:<input type="password" name = "password">
<input type = "submit" value="提交">
</form>
<hr size="8" color="red">

<h2>post方式的请求</h2>
<form action="" method="post">
用户名:<input type="text" name="name">
密&nbsp;码:<input type="password" name = "password">
<input type = "submit" value = "提交">
</form>

</body>


启动tomcat,在浏览器中输入:http://localhost/myday02/get_post.html
演示get:
结果一:
POST /myday02/get_post.html HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*
Referer: http://localhost/myday02/get_post.html
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost
Content-Length: 21
Connection: Keep-Alive
Cache-Control: no-cache


name=abc&password=123
User-Agent Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)
结果二:浏览器的地址栏内容变为:http://localhost/myday02/get_post.html?name=abc&password=123


在浏览器中输入:http://localhost/myday02/get_post.html
演示post:
结果一:
POST /myday02/get_post.html HTTP/1.1
Accept: */*
Referer: http://localhost/myday02/get_post.html
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost
Content-Length: 21
Connection: Keep-Alive
Cache-Control: no-cache


name=abc&password=123
结果二:浏览器的地址栏内容依然为:http://localhost/myday02/get_post.html

Http请求头详解


Accept: text/html,image/*      用于 告诉服务器,客户端支持的数据类型.
Accept-Charset: UTF-8          用于 告诉服务器,客户端支持的字符编码
Accept-Encoding: gzip,compress    告诉服务器客户机支持的数据压缩格式.  (重点)
Accept-Language: en-us,zh-cn   用户告诉服务器,客户机接受语言.
Host: www.it315.org:80          告诉服务器要访问那个主机
If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT   这个用来实现和缓存相关的, 跟last-modified. (重点)
Referer: http://www.it315.org/index.jsp   用来实现防盗链 (重点 )
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)    客户端的 类型.
Connection: close/Keep-Alive    用于连上服务器之后, 是否继续保持连接.



Http响应

一:概述:一个Http响应代表服务器向客户端回送的数据,它包括:
一个状态行,若干消息头,实体内容.
响应行:状态行用于描述服务器对请求的处理结果
多个消息头:用于描述服务器的基本信息,以及数据的描述,服务吕通过这些数据的描述信息,
 可以通知客户端如何处理等一会它回送来的数据.
一个空行:
实体内容:代表服务器向客户端回送的数据.
二:状态行:
格式:HTTP版本号  状态码  原因叙述<CRLF>
举例:HTTP/1.1 200 ok
状态码用于表示服务器对请求的处理结果,它是一个三位的十进制数.响应分为五类.

重定向Redirect





/servlet/RedTest


重定向:
示例代码:
在myday02中的myserver包中建立一个RedTest.java文件(Servler),内容为:
package myserver;


import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class RedTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 302 + location 实现请求重定向.
* 应用场景:一般用来做登陆.
* 特点:1.会发生二次请求.
*   2.浏览器地址栏会发生变化.
*/

response.setStatus(302);
response.setHeader("location", "/myday02/get_post.html");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
开启tomcat,在浏览器中输入:http://localhost/myday02/servlet/RedTest
其自动变成:http://localhost/myday02/get_post.html
此为重定向成功.
 00:00:00.000 0.018 139 GET 302 Redirect to http://localhost/myday02/get_post.html http://localhost/myday02/servlet/RedTest
 00:00:00.018 0.028 0 GET (Cache) text/html http://localhost/myday02/get_post.html

Refresh刷新

重定向:
示例代码:
在myday02中的myserver包中建立一个RefreshServelet.java文件(Servler),内容为:
package myserver;


import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class RefreshServelet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//refresh头实现定时刷新到某个页面.
//一般可用作定时刷新,
//应用:股票,聊天室,
// response.setHeader("Refresh", "2;url=http://www.baidu.com");
response.setHeader("Refresh", "1");

}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}


}



gzip压缩



压缩:


把conf/server.xml中的第72行那段数据改为:
 <Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" compressableMimeType="text/html,text/xml,text/plain" compression="on" />


压缩前: 00:00:00.000 0.038 11599 GET 200 text/html;charset=ISO-8859-1 http://localhost/
压缩后: 00:00:00.000 1.401 2296 GET 200 text/html;charset=ISO-8859-1 http://localhost/
注意:把server.xml修改前是压缩前,修改后把缓存清空后再访问才是压缩后的,



Tocmcat默认的缓存策略

If-Modified-Since,Last-Modified结合304实现。
第一次访问某个页面时,服务器将数据发给客户端,并在客户端生成一个缓存,同时记下本页面在服务器端被修改的时间:
Last-Modified: Sun, 05 May 2013 00:55:08 GMT 并发给客户端,
当客户端再次访问同一个页面时,将If-Modified-Since发给服务器,服务器读取If-Modified-Since中的时间,
If-Modified-Since: Sun, 05 May 2013 00:55:08 GMT
将此数据中的时间与Last-Modified中的时间作对比,如果相同,则说明这个页面自上次发给此客户端后一直没被修改,则服务器不再将数据发给客户端,
让客户端从自己的缓存中读取数据,
如果不同,则将数据发给客户端,并在客户端生成缓存,同时记下时间:Last-Modified,将本服务器中的这个页面最后一次修改的时间发给客户端,
这样既能节约带宽,又能提高响应时间。

Tomcat禁用缓存




Expires:-1
Cache-Control:no-cache
Pragma:no-cache




原文地址:https://www.cnblogs.com/riskyer/p/3266479.html