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
, 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
Directory Structure
After decompressing the Tomcat directory, you can see the following structure:
start server
To start Tomcat, you need to configure JDK environment variables in advance
- Configure the JAVA_HOME variable
- Add %JAVA_HOME%\bin to the path environment variable
Check whether the configuration is successful? ?
- open cmd
- 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
- 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.
Enter 127.0.0.1:8080 in the browser to see Tomcat's default welcome page:
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
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
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:
- 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 :
Modify the port content on line 69:
Deploy web applications
In the directory structure in front of this article, under the webapps folder: each folder is a webapp
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
Open the html file with VS Code:
<body>
<p>Test测试</p>
</body>
Then, access the path (make sure Tomcat is started):
Example 2:
Create html files in the ROOT directory
Open the html file with VS Code:
<body>
<p>ROOT目录下测试</p>
</body>
Then, access the path (make sure Tomcat is started):
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
access:
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:
- Open by path: