Tomcat and HTTP protocol

1. Java Web Overview

1.1 C / S structure

C / S structure is Client / Server, for example, QQ is C / S structure. Each of us needs to install the QQ client on the computer. The client software sends a request to the server software, and the server returns a response to the client.

However, because the client needs to be constantly updated, users are more troublesome to use. The client of the current C / S structure is much better, and the user can be prompted to update, and the user only needs to click update. At the earliest, users need to go to the official website to download the latest client, then uninstall the old version, and then install the new version.
Insert picture description here

1.2 B / S structure

B / S structure is browser / server, for example, websites are all B / S structure. Of course, the website is just one of the B / S architecture software. Online banking is also B / S structured!

The advantage of the B / S structure is that the user does not need to update the client, and the client only needs to have a browser to be OK. When the software needs to be updated, developers only need to care about the server side. Just like when you visit Baidu in your browser, you find that Baidu's homepage has changed, but you don't need to update anything.
Insert picture description here

1.3 Static webpage and dynamic webpage

In the early days, they were all static web pages on the Internet, namely html pages. The data of the static webpage cannot be changed automatically, for example, the current user information on the webpage: "Welcome XXX to log in to the system", which means that the current user name is XXX. If other users log in to the system, they should display the names of other users, and there is no need to modify the page! To put it bluntly, the user name should be a variable, not a constant! This cannot be done with static web pages!

Dynamic web pages can contain variables, so changes in data are not a problem. But dynamic web pages can only be used on the server side, and client browsers can only recognize static web pages. Therefore, if the user requests a dynamic webpage, then the server needs to convert the dynamic webpage into a static webpage and send it to the client browser!

Dynamic web pages must be handled by the server.

1.4 Common web pages

Static web pages: htm, html

Dynamic web pages: php, asp, aspx, jsp, .do, .action

1.5 Web server

The role of the Web server is to receive the client's request and respond to the client.
Well-known Java Web server:

  • Tomcat (Apache): used to develop and use; free, open source
  • JBoss (Redhat)
  • Weblogic (Orcale): It costs money! Developed using Tomcat, the project is written and put on Weblogic to run;
  • Websphere (IBM): It costs money! Similar to Weblogic.

2. Tomcat installation

2.1 Tomcat overview

The Tomcat server is provided by Apache and is open source and free. The best choice for development and learning. Before installing Tomcat, you need to install the JDK. In fact, no matter what kind of JavaWeb server you need to install the JDK. The Tomcat version we are using now can be Tomcat8.5 or Tomcat9.

Tomcat6 supports Servlet2.5;

Tomcat7, 8, 9 support Servlet3.0;

2.2 The role of Tomcat

After the user sends a request, Tomcat receives the request and returns a response to the user. Usually the user usually sends a request from the browser, and then the response of the browser is HTML code. Of course, this is not all. Sometimes users may send requests to the server by other means, and the response returned by Tomcat may not be html, it may also be a file, a picture, etc.
Insert picture description here

2.3 Tomcat installation

The decompressed version of Tomcat can be used only after decompression, without installation, so that multiple Tomcats can be installed on one computer.

Starting the unzipped version of Tomcat is a bit different, you need to first configure the environment variable: JAVA_HOME.

  • Startup: Find the startup.bat file in the bin directory of the Tomcat installation directory, and then double-click it.
  • Stop: Find the shutdown.bat file in the bin directory of the Tomcat installation directory, and then double-click it.

It is recommended to use the decompressed version! ! !
Insert picture description here

2.4 Tomcat test

To test Tomcat, enter in the browser:, http://localhost:8080or yes http://127.0.0.1:8080. 127.0.0.1Indicates the IP address of localhostthe machine, and the host name of the machine.

If other machines need to access, then localhostuse the machine's IP address instead.
If my IP is 192.168.1.100, you want to access Tomcat on my machine, you need to access in your browser: http://192.168.1.100:8080.
Insert picture description here

3. Tomcat analysis

3.1 Tomcat directory structure

Insert picture description here
It is necessary for us to understand the Tomcat directory structure, which helps us learn Tomcat better.

bin: This directory contains the executable file is under there startup.batand shutdown.batfiles startup.batused to start Tomcat, but you need to configure JAVA_HOMEand shutdown.batto stop Tomcat;

conf: This is a very, very important directory. There are three most important files in this directory:

  • server.xml: Configure the entire server information. For example, to modify the port number, the following will introduce this file in detail;
  • web.xml: Deployment descriptor file, many MIME types, namely document types, are registered in this file. These MIME types specify the document type between the client and the server. For example, if the user requests an html web page, the server will also tell the client browser that the response document is of the text / html type. The client browser knows how to handle it through this MIME type. Of course, this html file is displayed in the browser. But if the server responds to an exe file, then the browser cannot display it, but the download window should pop up. MIME is used to explain what type of document content is!

lib: Tomcat's class library, which contains a lot of jar files. If you need to add the jar file that Tomcat depends on, you can put it in this directory. Of course, you can also put the jar file that the project depends on in this directory. All the jar files in this directory can be shared;

logs: All log files in this directory record the startup and shutdown information of Tomcat. If there are errors when starting Tomcat, exceptions will also be recorded in the log files.

temp: Temporary files for Tomcat are stored. The contents in this directory can be deleted after stopping Tomcat!

webapps: A directory for storing web projects, where each folder is a project; if a directory already exists under this directory, then tomcat comes with it. project. Among them, ROOT is a special project. When the project directory is not given in the address bar, the corresponding is the ROOT project.

work: The files generated at run time, the final run files are here. Generated by the project in webapps! You can delete the contents of this directory, and when you run it again, the work directory will be generated again. When a client user accesses a JSP file, Tomcat generates a Java file through JSP, and then compiles the Java file to generate a class file. The generated java and class files are stored in this directory.

LICENSE:license.
NOTICE:description file.

You should have discovered that there is no need to give the project name when accessing the ROOT project. In fact, it localhostis a host name. Each host corresponds to a project space, localhostcorresponding to the Tomcat directory webapps. Each project space can have a project named ROOT. No need to give the project name when this ROOT project is accessed.

3.2 Tomcat modify the port number

Open the conf\server.xmlfile:
Insert picture description here
http default port number is 80, which means that port 80 is used when the port number is not given in the URL. Of course, you can also change to other port numbers. After modification, the server must be restarted. Port 80 is the default port. If port 80 is used for access, the port can be omitted. If we changed to port 80, you can not access the port number plus: http://localhost/.
Insert picture description here

4. Web project

4.1 Directory structure after web project deployment

The webapps directory is used to store Java projects, and each folder is a project. By default, there are already four projects in this directory, all of which come with tomcat. ROOT is the main Tomcat project that we visited when testing Tomcat.

To create our own project, we need to follow the JavaEE directory structure specification. The following is the directory structure of the Hello project:

Hello(必须出现的)
	|------ 存放web资源。(jsp, xml, HTML  CSS  JS 图片...)
	|------	WEB-INF     (必须出现的)
		|------ web.xml 	(必须出现的)
		|------ classes		Java类的编译路径(Servlet、自定义类 )
		|------ lib			需要引入的第三方的jar包.

HelloIt is a project directory. There must be a WEB-INFdirectory named (must be capitalized) under the project directory, and there WEB-INFmust be a web.xmlfile under the directory .

WEB-INFThe directory is a protected directory. The following things are not directly accessible to users, but dynamic pages are accessible. Usually WEB-INFthere will be liband classes, libunder the jar file classesrequired by the project, the class file required by the project exists in the directory! If you directly access WEB-INFit, you will not find the path:
Insert picture description here
web.xmlonly the XML document declaration and root element are required in the file.
Insert picture description here

4.2 Tomcat release project

4.2.1 Under the webapps folder

Put the project webappsunder the directory, that is, the project space directory corresponding to the localhost host name. Visit:http://localhost/Hello/hello.html .

4.2.2 under any directory

You can put the project in any directory, and then <Context>specify the actual location of the project by adding elements. In the <Host>adding element <Context>the element, and then specify the <Context>path and element docBaseattributes, which pathspecify project virtual path, it can be given arbitrarily, for example, abc. docBaseSpecify the actual storage path of your project.

For example, the real directory of the project is there D:\demo\webapp\hello. You find server.xmlthe <Host>element in, add sub-elements to it <Context>, the content is as follows:
Insert picture description here
Note that once docBasethe value of the attribute is given, when accessing localhostthe abc item, you will not go to the webappsdirectory to find the item, but docBaselock the item by the value Location, ie F:\hello1.

Note that the URL to access the project: http://localhost/h1/index.jspinstead http://localhost/hello1/index.jsp, you might say that our project directory is called hello1, but because <Context>the path attribute of the element is specified , the project directory name will no longer be used as the access path when accessing the project.

5. HTTP protocol

5.1 HTTP protocol concept

HTTP, the Hypertext Transfer Protocol. All browsers can issue http protocol.

Agreement: It is the prescribed text format! For example, there must be a format for writing a book, and a format for writing a summary. Of course, the request sent by the client must also have a format. This format is the agreed format, so the server obtains the request information according to the request format, and then the server responds according to the response format. Of course, the client also parses according to the format of the response .

5.2 Stateless protocol

HTTP is a stateless protocol, you must understand this! ! !

The so-called stateless is that the client sends a request, the server returns a response, and then disconnects.

The benefit of the stateless protocol is, of course, high efficiency. The user disconnects as soon as the user connects. When the user sends a request again, the user reconnects, and then disconnects again after the response. If the user finds the novel he wants to read on the server, then the server just sends the content (html) of the novel to the user's browser, and then the user's browser shows that it has been sent to the local html. Connected. The user may watch for a long time, but this will not occupy any resources of the server!

5.3 HTTP1.0 and HTTP1.1

There are now two versions of HTTP, HTTP1.0 and HTTP1.1.

HTTP1.0: disconnect immediately after the response;

HTTP1.1: The Host header information must be included in the request. It will not be disconnected immediately after the response. There will be a timeout period. Multiple requests / responses may be completed before the timeout. When the timeout expires, it will be disconnected. .

GET /web_demo/index.jsp HTTP/1.0
Host: localhost:8080

5.4 Request protocol and response protocol

Both the request protocol and the response protocol are composed of the following parts:

请求(或响应)首行;
请求(或响应)头信息;
空行;
请求(或响应)正文。 

5.5 Request Agreement

We can monitor the request and response through the browser to obtain complete request and response information.

  • Fire Fox:
  • Google: This feature is built in, and there is no need to install it.

Request
GET /hello/index.jsp HTTP / 1.1
response
Insert picture description here

5.5.1 GET request

The following is the request information obtained through FireFox, this is the content of all the request information sent by the browser to the server. Requested address http://localhost/hello/index.jspis: .

//请求首行
GET /hello/index.jsp HTTP/1.1
//下面全部是请求头信息,因为GET请求没有正文
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Cookie: JSESSIONID=369766FDF6220F7803433C0B2DE36D98
//空行,因为GET没有正文,所以下面没有东西了
  • GET /hello/index.jsp HTTP/1.1: GET request, request server path is /hello/index.jsp, protocol is 1.1;
  • Host:localhost: The requested host name localhost;
  • User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0: Information related to browser and OS. Some websites will display the user's system version and browser version information, which are all obtained by obtaining the User-Agent header information;
  • Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8: Tell the server that the type of documents that the current client can receive, in fact, contains * / * here, it means that everything can be received;
  • Accept-Language: zh-cn,zh;q=0.5: The language supported by the current client can be found in the tool  option of the browser;
  • Accept-Encoding: gzip, deflate: Supported compression format. When data is transmitted on the network, the server may compress the data before sending it;
  • Connection: keep-alive: The link method supported by the client, keep the link for a period of time, the default is 3000ms;
  • Cookie: JSESSIONID=369766FDF6220F7803433C0B2DE36D98: Because this is not the first time to visit this address, the cookie sent from the previous server response will be sent in the request together; the name of this cookie is JSESSIONID, and then it is mentioned in the conversation!
  • The Get request has no body, only header information, and the request parameters are passed in the url of the first line of the request. The requested parameter capacity is limited and cannot exceed 1024kb

5.5.2 POST request

// 请求首行
POST /hello/index.jsp HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http://localhost/hello/index.jsp
Cookie: JSESSIONID=369766FDF6220F7803433C0B2DE36D98
Content-Type: application/x-www-form-urlencoded 
Content-Length: 14 
// 这里是空行
//POST有请求正文
username=hello
  • Referer: http://localhost/hello/index.jsp: Which page the request came from. This value indicates that the request was sent from the index.jsp page! This stuff is still useful! If the role of this form is to download, you can determine which page the request was sent from before providing resources to the user. If it is not sent from this website, you can refuse to download, such as stolen chain!
  • Content-Type: application/x-www-form-urlencoded: The data type of the form, indicating that UTF-8 encoded data in url format will be used; url-encoded data are prefixed with "%", followed by two-digit hexadecimal;
  • Content-Length:14: Indicates the length of the requested data, here indicates 14 bytes.
  • username=hello: This is the request text! hello is the data entered in the form, and username is the name of the text box.

5.6 Response protocol

Response information:

// 响应首行
HTTP/1.1 200 OK
// 响应头信息
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Content-Length: 777
Date: Sat, 09 Feb 2012 18:30:52 GMT
// 空行,下面是响应正文,即HTML代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="http://localhost:80/hello/">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    This is my JSP page. <br>
    
    <form method="post" action="index.jsp">
    	<input type="text" name="username" /><br/>
    	<input type="submit" value="SUBMIT"/>
    </form>
  </body>
</html>
  • HTTP/1.1 200 OK: The response protocol is HTTP1.1, the status code is 200, indicating that the request was successful, and OK is the interpretation of the status code;
  • Server: Apache-Coyote/1.1: This is the version information of the server;
  • Content-Type: text/html;charset=UTF-8: The encoding used in the request body is UTF-8;
  • Content-Length: 777: The content of the response is 777 bytes;
  • Set-Cookie: JSESSIONID=C97E2B4C55553EAB46079A4F263435A4; Path=/hello: Cookie that responds to the client;
  • Date: Sat, 09 Feb 2012 18:30:52 GMT: Response time, which may have a time zone difference of 8 hours;

Request: GET / POST

Only forms can be POST, others are GET

POST has a body, which is the form content:username=zhangSan&password=123

There is no text for GET, but there are blank lines!

Request first line: (GET/POST) URL HTTP/1.1
Response first line: HTTP/1.1 状态码 状态码的解释
The body of the response is HTML!

Post request: There is a text, and the content of the text is not limited, submit through the form (except for the post request when the form is submitted, the rest are get requests).

Insert picture description here

Published 94 original articles · liked 0 · visits 722

Guess you like

Origin blog.csdn.net/qq_46578181/article/details/105344312