Basic use of Tomcat and project deployment.

Table of contents

1. HTTP protocol

1. Basic introduction

2. Request data format

3. Response data format

2. Tomcat  

1. Basic introduction

2. Basic use

3. Project deployment 


1. HTTP protocol

1. Basic introduction

▶ Overview

   HTTP protocol (HyperText Transfer Protocol): It mainly defines communication rules. It is a hypertext transfer protocol that specifies the rules for data transmission between the browser and the server.

   The rules of data transmission mean that request data and response data need to be transmitted in a specified format.


   The browser sends a request to the server, and the server responds with data to the browser. The whole process needs to abide by certain rules. TCP, UDP, etc. are all rules. Here we need to use the HTTP protocol, which is also a rule. If you want to know the specific format, you can open the browser, click `F12` to open the developer tool, click `Network` to view the specific format content of the request data and response data for a certain request, as shown below:

▶ HTTP features

  ▷ Based on TCP protocol: connection-oriented, safe. TCP is a connection-oriented (three-way handshake is required before establishing a connection), reliable, byte-stream-based transport layer communication protocol, and is safer in terms of data transmission.

  ▷ Based on the request-response model: a request corresponds to a response, and there is a one-to-one correspondence between requests and responses.

  ▷ HTTP protocol is a stateless protocol: it has no memory ability for transaction processing. Each request-response is independent. Stateless means that after the client sends an HTTP request to the server, the server responds to the data according to the request. After the response, no information is recorded. This feature has both advantages and disadvantages: ① Disadvantage: Data cannot be shared between multiple requests; ②Advantage: Fast

▶ Problems caused by inability to share data between requests

  For example: Jingdong shopping, `Add to shopping cart` and `Go to shopping cart to checkout` are two requests, the stateless nature of the HTTP protocol, after the response to the request to add to the shopping cart, it does not record which product was added to the shopping cart to initiate the shopping cart After the settlement request, because it is impossible to obtain which products have been added to the shopping cart, the data cannot be displayed correctly in this request.

   When using it, we found that JD.com can display data normally, because Java has already considered this problem, and proposed to use `session technology (Cookie, Session)` to solve this problem.

2. Request data format

▶ brief introduction

  The request data is divided into three parts in total, namely the request line, request header, and request body:

  ▷ Request line: The first line of data in the HTTP request. The request line contains three pieces of content, which are GET [request method] / [request URL path] HTTP/1.1 [HTTP protocol and version]. There are seven request methods. Commonly used are GET and POST.

  ▷ Request header: the second line starts, and the format is key: value. The request header will contain several attributes. The common HTTP request headers are:

   ● Host: Indicates the host name of the request
   ● User-Agent: Browser version, for example, the logo of Chrome browser is similar to Mozilla/5.0 ...Chrome/79; the logo of IE browser is similar to Mozilla/5.0 (Windows NT ...) like Gecko;
   ● Accept: Indicates the resource type that the browser can receive, such as text/*, image/* or */* means all;
   ● Accept-Language: Indicates the language preferred by the browser, and the server can return different languages ​​accordingly Web pages;
   ● Accept-Encoding: Indicates the compression type supported by the browser, such as gzip, deflate, etc.

  ▷ Request body: the last part of the POST request, storing request parameters:

   As shown in the above figure, the content of the red line box is the content of the request body, and there is a blank line between the request body and the request header. At this time, the browser sends a POST request. The difference between GET and POST two requests:

   ○ GET request: The request parameters are in the request line, there is no request body, and the size of the request parameters is limited

   ○ POST request: The request parameters are in the request body, and there is no limit to the size of the request parameters
 

▶ What is the use of request header data?

  For example: The server can obtain the relevant information of the client according to the content in the request header. With this information, the server can handle different business requirements, such as:

  ● The results of parsing HTML and CSS tags by different browsers will be inconsistent, so the same code will have different effects in different browsers.
  ● The server obtains the client's browser type according to the data in the client's request header, and can set different codes according to different browsers to achieve consistent results.
  ● This is what we often call browser compatibility issues.

3. Response data format

▶ brief introduction

  The response data is divided into three parts in total, namely the response line, response header, and response body:

  ▷ Response line: the first line of response data, the response line contains three pieces of content, which are HTTP/1.1[HTTP protocol and version] 200[Response status code] ok[Description of status code]

  ▷ Response header: start from the second line, the format is key: value. The response header will contain several attributes. The common HTTP response headers are:

   ● Content-Type: indicates the type of the response content, such as text/html, image/jpeg;
   ● Content-Length: indicates the length (number of bytes) of the response content;
   ● Content-Encoding: indicates the response compression algorithm, such as gzip;
   ● Cache-Control: Indicates how the client should cache, for example max-age=300 means it can be cached for up to 300 seconds

  ▷ Response body: The last part stores the response data. The content of <html>...</html> in the figure above is the response body, which is separated from the response header by a blank line.

▶ Response status code

Three common response status codes:

  ● 200 ok: the client request is successful
  ● 404 Not Found: the requested resource does not exist
  ● 500 Internal Server Error: an unexpected error occurred on the server


2. Tomcat  

1. Basic introduction

▶ Web Overview
  The Web is a global wide area network, also known as the World Wide Web (www), a website that can be accessed through a browser. In our daily life, we often use browsers to visit websites such as `Baidu`, `Jingdong`, `Chuanzhi official website`, etc. These websites are collectively referred to as Web sites.
▶ B/S architecture
  B/S architecture: Browser/Server, browser/server architecture mode, its characteristic is that the client only needs a browser, and the logic and data of the application are stored on the server. The browser only needs to request the server to obtain web resources, and the server sends the web resources to the browser.

  Let’s recall our usual online process: open a browser to visit the Baidu homepage, enter the content to be searched, and click Enter or click Baidu to obtain and search related content. Thinking about the content of the search is not on our own point, so where does this content come from? The answer is obviously returned to us from the Baidu server; the daily small details of Baidu, the logo of Baidu will change to different pictures during the holidays, the server will change, and the client can get the latest content without doing tasks, so we say
  B Benefits of the /S architecture: Easy to maintain and upgrade (after the server is upgraded, the client can use the new version without any deployment).

▶ Static resources
  Static resources mainly include HTML, CSS, JavaScript, pictures, etc., and are mainly responsible for the display of the page. However, since the content produced is static, the content seen by all people will be exactly the same.
  In the process of daily surfing the Internet, in addition to seeing these beautiful pages, we will also encounter a lot of dynamic content, such as our common Baidu login effect: `Zhang San`, after logging in, what we see in the upper right corner of the webpage is `Zhang Three`, and `Li Si` will see `Li Si` after logging in. Therefore, most of the content seen by different users accessing the same resource is different. To achieve such an effect, static resources alone cannot be achieved.

▶ Dynamic resources
  Dynamic resources mainly include Servlet, JSP, etc., which are mainly used for logic processing. After the dynamic resource processes the logic, it will hand over the obtained result to the static resource for display, and the dynamic resource and the static resource should be used together.

▶ Database
  The database is mainly responsible for storing data. The entire Web access process is shown in the figure below:

(1) The browser sends a request to the server to request the required related resources;
(2) Resources are divided into dynamic resources and static resources, and dynamic resources can use Java code Content written in accordance with the specifications of Servlet and JSP;
(3) Business processing can be performed in Java code and data can be read from the database;
(4) After getting the data, give the data to the HTML page for display, and then combine CSS and JavaScript makes the display effect better;
(5) The server responds to the browser with static resources;
(6) The browser parses these resources;
(7) After parsing, the effect is displayed on the browser, and the user can see the final result.

▶ Web server

 What is a web server?

  A web server is an application program (software) that encapsulates the operations of the HTTP protocol, so that programmers do not need to directly operate the protocol, making web development more convenient. The main function is to "provide online information browsing service". The Web server is a software installed on the server side. In the future, we will deploy the Web project we wrote to the Web Tomcat server software. When the Web server software is started, the pages deployed in the Web server software can be directly accessed through the browser. up.

▶ Tomcat overview

  Tomcat is a core project of the Apache Software Foundation. It is an open source and free lightweight web server that supports a small number of JavaEE specifications for Servlet/JSP. Because Tomcat supports the Servlet/JSP specification, Tomcat is also called a Web container and a Servlet container. Servlet needs to rely on Tomcat to run.

        Official website:  https://tomcat.apache.org/

▶ JavaEE specification

  Java Enterprise Edition, Java Enterprise Edition. Refers to the sum of technical specifications for Java enterprise-level development. Contains 13 technical specifications: JDBC, JNDI, EJB, RMI, JSP, Servlet, XML, JMS, Java IDL, JTS, JTA, JavaMail, JAF.

2. Basic use

▶ Download and install

  1. Go to the official website to download the compressed package of the corresponding version.

  2. Decompress the compressed package directly and the installation is successful.

   Note: When Tomcat decompresses, the directory where the decompression is located can be arbitrary, but it is best to decompress to a directory that does not contain Chinese and spaces, because when deploying the project later, if the path contains Chinese or spaces, the program deployment may fail. .

  3. Open the `apache-tomcat-8.5.68` directory to see the following directory structure:

  ▷ bin: There are two types of files in the directory, one ends with `.bat`, which is the executable file of Windows system, and the other ends with `.sh`, which is the executable file of Linux system.

  ▷ webapps: It is the directory where the project will be deployed in the future.

▶ start

 ▷ Double-click: bin\startup.bat

 ▷ After starting, visit `http://localhost:8080` through the browser and see the content of Apache Tomcat, which means that Tomcat has started successfully.

  ▷ Note: During the startup process, the console has Chinese garbled characters, and the files in the conf/logging.properties directory need to be modified: UTF-8 --> GBK

   ▷ Flashback problem:

 When Tomcat starts, the startup window flashes: You need to check whether the JAVA_HOME environment variable is configured correctly

▶ close

 ▷ There are three ways to switch off:

  ● Directly X off the running window: forced to close. [NOT RECOMMENDED]
  ● bin\shutdown.bat: graceful shutdown.
  ● ctrl+c: normal shutdown. [suggestion]

▶ Port configuration

 ▷ The default port of Tomcat is 8080. If you want to modify the port number started by Tomcat, you need to modify conf/server.xml

   Note: The default port number of the HTTP protocol is 80. If you change the Tomcat port number to 80, you will not need to enter the port number when accessing Tomcat in the future.

 ▷ Possible errors during startup

  The value range of Tomcat's port number is any unoccupied port between 0-65535. If the set port number is occupied, the following error will be included when starting: 

3. Project deployment 

▶ Tomcat deployment project

  Place the project in the webapps directory, and the deployment is complete. Generally, JavaWeb projects will be packaged into a war package, and then put the war package in the Webapps directory. Tomcat will automatically decompress the war file. After Tomcat detects the war package, it will automatically complete the decompression, and there will be an additional project directory in the webapps directory. , visit `http://localhost/project path` through a browser, and if you can see the following content, it means that the project has been deployed successfully.

▶ Web project structure

 The structure of the Web project is divided into: a project under development and a Web project that can be deployed after development. The structures of these two projects are different.

 ▷ Maven Web project structure: projects under development

 ▷ Web projects that have been developed and deployed

 

  ● The development project can obtain the deployed Web project directory by executing the Maven packaging command package
  ● The compiled Java bytecode files and resources resource files will be placed in the classes directory under WEB-INF
  ● pom.xml The jar package corresponding to the dependent coordinates will be placed in the lib directory under WEB-INF

▶ Create a Maven Web project (two ways)

 ▷ Using the skeleton, the specific steps include:
  1. Create a Maven project
  2. Choose to use the Web project skeleton

  3. Enter the Maven project coordinates to create the project

  4. After confirming the Maven-related configuration information, complete the project creation

  5. Delete the redundant pom.xml Content

 6. Completing the missing directory structure of the Maven Web project

 

 ▷ Without using a skeleton, the specific steps include:

  1. Create a Maven project 

 2. Choose not to use the Web project skeleton

  3. Enter the Maven project coordinates to create the project

   4. Set the packaging method in pom.xml to war, and the default is not written to represent the packaging method as jar 

  5. Complete the directory structure of the missing webapp in the Maven Web project 

 6. Completing the missing directory structure of WEB-INF/web.xml in the Maven Web project 

7. After completion, the final project structure is as follows:

▶ IDEA use Tomcat

There are two ways to integrate Tomcat in IDEA, namely integrating local Tomcat and Tomcat Maven plug-ins.

▷ Integrate local Tomcat: Integrate the locally installed Tomcat8 into IDEA to complete project deployment.

 1. Open the panel to add local Tomcat

 2. Specify the specific path of the local Tomcat 

 3. Modify the name of Tomcat, this step can not be changed, just to make the name look more meaningful, the port in the HTTP port can also be modified, for example, change 8080 to 80

 4. Deploy the development project to Tomcat

 Extended content: What is the difference between the two deployment project modes of xxx.war and xxx.war exploded?

   ● The war mode is to pack the WEB project into a war package and publish the war package to the Tomcat server

   ● The war exploded mode is to publish the WEB project to the Tomcat server based on the location relationship of the current folder
   ● After the war mode is successfully deployed, the deployed project content will be in Tomcat’s webapps directory
   ● After the war exploded mode is deployed successfully, the Tomcat’s webapps directory It does not exist, but uses the content in the target directory of the project for deployment
   . It is recommended that everyone choose the war mode for deployment, which is more in line with the actual situation of project deployment.

 5. After the deployment is successful, you can start the project. In order to better see the effect of the startup, you can add a.html page in the webapp directory

 6. After the startup is successful, you can conduct an access test through the browser

  7. Final Considerations 

  

▷ Tomcat Maven plugin

 1. Add Tomcat plugin in pom.xml

<build>
       <plugins>
       	<!--Tomcat插件 -->
           <plugin>
               <groupId>org.apache.tomcat.maven</groupId>
               <artifactId>tomcat7-maven-plugin</artifactId>
               <version>2.2</version>
           </plugin>
       </plugins>
   </build>

2. Use the Maven Helper plug-in to quickly start the project, select the project, right click --> Run Maven --> tomcat7:run

 Note: If you can't see Run Maven and Debug Maven after selecting the project and right-clicking, you need to download the Maven Helper plug-in in IDEA at this time. The specific operation method is: File --> Settings --> Plugins --> Maven Helper ---> Install, after installation, follow the prompts to restart IDEA, and you can see it. 

The Maven Tomcat plug-in currently only has the Tomcat7 version, and no higher version can be used. Using the Maven Tomcat plug-in, if you want to modify the port and access path of Tomcat, you can directly modify pom.xml 

<build>
    <plugins>
    	<!--Tomcat插件 -->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
            	<port>80</port><!--访问端口号 -->

                <!--项目访问路径
					未配置访问路径: http://localhost:80/tomcat-demo2/a.html
					配置/后访问路径: http://localhost:80/a.html
					如果配置成 /hello,访问路径会变成http://localhost:80/hello/a.html
				-->
                <path>/</path>
            </configuration>
        </plugin>
    </plugins>
</build>

Guess you like

Origin blog.csdn.net/yzh2776680982/article/details/126799542