HTTP protocol
HTTP, HyperText Transfer Protocol (HyperText Transfer Protocol) is the most widely used network protocol on the Internet. All www files must comply with this standard. HTTP was originally designed to provide a way to publish and receive HTML pages
The composition of the Http protocol
Http protocol consists of Http request and Http response . When you enter a URL in the browser to visit a website, your browser will encapsulate your request into an Http request and send it to the server site. After the server receives the request, it will organize the response The data is encapsulated into an Http response and returned to the browser, that is, there is no response without a request
HTTP request
Edit a form page of form.html as follows:
Click the submit button to capture the package as follows:
1) Request line
Request method: POST, GET
Request resource: /DemoEE/form.html
Protocol version: HTTP/1.1
HTTP/1.0, send a request, create a connection, get a web resource, disconnect
HTTP/1.1, send a request, create a connection, get multiple web resources, keep the connection
2) Request header
Some information sent by the client to the server in the request header, using a key-value pair to represent key:value
Common request headers |
Description (red master, other understand) |
Referer |
The browser informs the server where the current request is coming from. If it is direct access, this header will not be present. Commonly used for: anti-theft chain |
If-Modified-Since |
The browser notifies the server of the last change time of the local cache. Combined with another response header controls the caching of browser pages. |
Cookie |
Session-related technologies used to store cookie information cached by browsers. |
User-Agent |
The browser notifies the server, the client browser and operating system related information |
Connection |
Stay connected. Keep-Alive connection, close has been closed |
Host |
The requested server hostname |
Content-Length |
the length of the request body |
Content-Type |
If it is a POST request, there will be this header. The default value is application/x-www-form-urlencoded, indicating that the content of the request body is url encoded |
Accept: |
The MIME types supported by the browser. A way of describing a file type. MIME format: big type/small type[;parameters] E.g: text / html, html text text/css, css files text/javascript, js files image/*, all image files |
Accept-Encoding |
The browser informs the server of the data compression formats supported by the browser. Such as: GZIP compression |
Accept-Language |
The browser informs the server of the languages supported by the browser. Various languages (internationalized i18n) |
3) Request body
When the request method is post, the request body has the requested parameters in the following format:
username=zhangsan&password=123
When the request method is get, the request parameters will not appear in the request body, but will be spliced after the url address
http://localhost:8080...?username=zhangsan&password=123
Http response
1) Response line
HTTP protocol
status code:
200: The request was successful
302: Request redirection
304: The requested resource has not changed, access the local cache
404: The requested resource does not exist, usually the user path is written incorrectly, or the server-side resource may be deleted
500: Internal server error, usually the program throws an exception.
Status information: Status information changes according to the status code change
2) Response header
The responses are also in the form of key-value pairs, and the server returns the information to the client in the form of key-value pairs
Common request headers |
describe |
Location |
Specifies the path of the response, which needs to be used in conjunction with the status code 302 to complete the jump. |
Content-Type |
Type of response body (MIME type) Value: text/html;charset=UTF-8 |
Content-Disposition |
Parse the body in download mode via browser 取值:attachment;filename=xx.zip |
Set-Cookie |
与会话相关技术。服务器向浏览器写入cookie |
Content-Encoding |
服务器使用的压缩格式 取值:gzip |
Content-length |
响应正文的长度 |
Refresh |
定时刷新,格式:秒数;url=路径。url可省略,默认值为当前页。 取值:3;url=www.itcast.cn //三秒刷新页面到www.itcast.cn |
Server |
指的是服务器名称,默认值:Apache-Coyote/1.1。可以通过conf/server.xml配置进行修改。<Connector port="8080" ... server="itcast"/> |
Last-Modified |
服务器通知浏览器,文件的最后修改时间。与If-Modified-Since一起使用。 |
3)响应体
响应体是服务器回写给客户端的页面正文,浏览器将正文加载到内存,然后解析渲染显示页面内容
Tomcat服务器
(1)B/S系统和C/S系统
Brower/Server:浏览器 服务器 系统-----网站
Client/Server:客户端 服务器 系统-----QQ、飞秋、大型游戏
(2)web应用服务器
供向外部发布web资源的服务器软件
(3)web资源
存在于web应用服务器可供外界访问的资源就是web资源
例如:存在于web应用服务器内部的Html、Css、js、图片、视频等
1)静态资源:指web页面中供人们浏览的数据始终是不变。比如:HTML、CSS、 JS、图片、 多媒体。
2)动态资源:指web页面中供人们浏览的数据是由程序产生的,不同时间点访问 web页面看到的内容各不相同。比如:JSP/Servlet、ASP、PHP
javaWEB领域:动态资源认为通过java代码去动态生成html
(4)请求和响应
(5)请求的URL地址
web开发中常用的web应用服务器
1)weblogic:oracle公司的大型收费web服务器 支持全部javaEE规范
2)websphere:IBM公司的大型收费web服务器 支持全部的javaEE规范
3)Tomcat:Apache开源组织下的 开源免费的中小型的web应用服务器 支持 javaEE 中的servlet 和 jsp规范
Tomcat的下载与安装
(1)下载Tomcat
官网地址:http://tomcat.apache.org/whichversion.html
(2)安装Tomcat
Tomcat有安装版和解压版(绿色版)
安装版以.exe形式的安装包,双击安装到我们的电脑上,用的比较少
解压版,即绿色版,解压后直接使用,用的比较多
Tomcat的目录结构
bin:脚本目录
启动脚本:startup.bat
停止脚本:shutdown.bat
conf:配置文件目录 (config /configuration)
核心配置文件:server.xml
用户权限配置文件:tomcat-users.xml
所有web项目默认配置文件:web.xml
lib:依赖库,tomcat和web项目中需要使用的jar包
logs:日志文件.
localhost_access_log.*.txt tomcat记录用户访问信息,星*表示时间。
例如:localhost_access_log.2016-02-28.txt
temp:临时文件目录,文件夹内内容可以任意删除。
webapps:默认情况下发布WEB项目所存放的目录。
work:tomcat处理JSP的工作目录。
Tomcat的启动与运行
双击Tomcat下的bin下的startup.bat启动Tomcat
在浏览器的地址栏中输入http://localhost:8080,看到如下页面证明启动成功
Tomcat启动不成功的原因分析:
1)如果没有配置JAVA_HOME环境变量,在双击“startup.bat”文件运行tomcat 时,将一闪立即关闭。且必须配置正确,及JAVA_HOME指向JDK的安装目录
2)端口冲突
java.net.BindException: Address already in use: JVM_Bind <null>:8080
修改Tomcat/conf/server.xml
web应用的目录结构
注意:WEB-INF目录是受保护的,外界不能直接访问
使用Eclipse绑定Tomcat并发布应用
1:获得服务器运行环境配置,Window/Preferences/Server/Runtime Environmen
2:添加服务器
3:选择服务器在硬盘的地址,然后所有的都是确定/Next/Finish
4:完成成功
5:设置发布位置
修改tomcat发布的位置
6:项目右键/Run As/Run on Server