Tomcat【HTTP server】

Preface: When you see Tomcat, you know the Tom Cat in Tomcat and Mouse, but Tomcat here has nothing to do with the "Tom Cat" in Tomcat and Mouse. Tomcat is essentially a web server


Before, we have learned the HTTP protocol , which is the format
of

Download and install

Download it on the Tomcat official website
insert image description here
, download it to the location you want to store, and then unzip it. NoteThe decompressed directory is best not to contain " Chinese " or special symbols or spaces

insert image description here

Directory Structure

After decompressing the Tomcat directory, you can see the following structure:

insert image description here

start server

To start Tomcat, you need to configure JDK environment variables in advance

  1. Configure the JAVA_HOME variable

insert image description here

  1. Add %JAVA_HOME%\bin to the path environment variable

insert image description here

Check whether the configuration is successful? ?

  • open cmd

insert image description here

  • Enter "%java_home%/bin/java" -version
    (equivalent to finding the java.exe file through an absolute path)

If the following content is displayed, it proves that the configuration is complete

insert image description here

  1. Run the Tomcat program: start

In the bin directory, double-click startup.bat to start the Tomcat server
. If a small black window appears after double-clicking, it disappears after a flash. It may be that the environment variable is not configured properly.
insert image description here

insert image description here
Enter 127.0.0.1:8080 in the browser to see Tomcat's default welcome page:

insert image description here
If you can't see the welcome page, check whether the IP address and port number of the URL are correct, and also check whether Tomcat has started
successfully

What to do if startup fails?

The most common reason for startup failure is that the port number is occupied.
When Tomcat starts, it will bind ports 8080 and 8005 by default. If any other process has bound any of these two ports, Tomcat will not start.

How to check whether a certain port is occupied (through the port number query program)

  • Windows: netstat -ano | findsr port number
  • Linux: netstat -anp | grep port number

insert image description here
The above results show that port 8080 is already occupied, and the process is 21036.

Open Task Manager to view

Tomcat is also a program developed based on Java, so after running, what you see is the java.exe program

insert image description here
The method to solve the port is occupied:

  • Close the occupied program, and then open Tomcat

Find the occupied port in the task manager, then right-click to end the task, such as:

insert image description here

  • If the program that occupies the port still wants it to continue running, modify the port that Tomcat starts binding

Double click: conf/server.xml :

insert image description here
Modify the port content on line 69:

insert image description here

Deploy web applications

In the directory structure in front of this article, under the webapps folder: each folder is a webapp

insert image description here
The default page of Tomcat is the page under the ROOT folder. If you deploy it yourself, you need to enter the name of the folder as the path, but the name of the ROOT folder does not need to be entered.

Example 1:
Create folder Test in webapps, and then create an html file

insert image description here
Open the html file with VS Code:

<body>
    <p>Test测试</p>
</body>

Then, access the path (make sure Tomcat is started):

insert image description here
Example 2:
Create html files in the ROOT directory

insert image description here
Open the html file with VS Code:

<body>
    <p>ROOT目录下测试</p>
</body>

Then, access the path (make sure Tomcat is started):

insert image description here

Notice:

  • In the webapps directory, each folder is a webapp
  • In the webapps directory, you cannot directly place html files to access
  • Access path: non-ROOT directory, with the name of the webapp folder; ROOT directory, without the folder name
  • / By default, the resource of index.html will be searched
  • The name in the webapp folder, without Chinese, spaces and special characters

insert image description here
access:

insert image description here
Thinking: Double-click the .html file to open the web page, why use the ip:port/path access with hierarchy, what is the difference? ?

  • Double click to open:

insert image description here

  • Open by path:

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/m0_47988201/article/details/123001602