Server software: apache-tomcat-8.5.27
Operating system: Windows 10 64-bit Home Edition
Table of contents
1. What is a web server?
constitute:
硬件
: Computer , providing services for other customer computers to access软件
: The server software installed on the computer can provide services to other computers in the network after installation, and map local files into a virtual url address for other people in the network to access.
effect:
The web server is mainly used to receive requests sent by clients and respond to client requests.
Common Java web servers: Tomcat (Apache), JBoss (Redhat), etc.
2. What is the Tomcat server?
Tomcat是一个开源的Web服务器软件
, which is a subproject of the Apache Foundation.
Tomcat is mainly used to run web applications on the Java platform, such as Java Servlet, JavaServer Pages (JSP), etc.
Tomcat includes an HTTP server and a Servlet container, which can package Java Web applications into WAR files and deploy them to Tomcat. When a user requests a web application, Tomcat processes the request and provides a response, allowing the user to access pages and functions in the web application.
3. How to install Tomcat server locally?
3.1 Download and install Tomcat
step:
①Click to open Tomcat official website , select Tomcat8 version
②Choose the Tomcat decompression version corresponding to your computer operating system version, and then click Download . My computer is win10 64-bit operating system, so I chose 64-bit Windows zip (pgp, sha512)
③Put the downloaded Tomcat zip package to the D drive, and then right-click to decompress it to the current folder
Notice:
将压缩包放在一个无中文无空格的目录下,避免出现中文乱码问题
3.2 Prerequisites for using Tomcat
需要正确配置]AVA_HOME的环境变量(因为需要Java的运行环境)
If it is not configured, or the configuration is wrong, starting the server will fail
3.3 How to configure the Java_HOME environment variable? (If configured, this section can be ignored)
① Double-click to open on the desktop
计算机
, find it on the left after entering计算机
, click the mouse右键
, and select属性
, as shown in the figure:
②In the pop-up system setting interface, select on the right
高级系统设置
, select环境变量
and open, as shown in the figure:
③ In
系统变量
, click新建
to create a new environment variable, as shown in the figure:
④ Enter the variable name
JAVA_HOME
, enter the variable value into your local Java jdk installation path, and click确定
, as shown in the figure:
⑤Select
Path
the environment variable,双击
or点击编辑
, as shown in the figure:
⑥Click
新建
, type%JAVA_HOME%\bin
and;%JAVA_HOME%\jre\bin
semicolon must be written, and it must be an English symbol , and then click确定
. as the picture shows:
⑦ After editing the environment variable,
管理员身份运行cmd命令行
type inJavac
the command to test, if no error is reported, it means success, as shown below:
3.4 Start/stop Tomcat server
①Start the Tomcat server
In the bin directory under the decompression path of the Tomcat software, double-click to
startup.bat
access in the address bar of the browser to test, as shown in the following figure:http://localhost:8080
Notice:
a. If the above black box flashes when you start the Tomcat server, it is probably because your Java_HOME environment variable is not properly configured
startup.bat will call catalina.bat, and catalina.bat will call setclasspath.bat, and setclasspath.bat will use the JAVA_HOME environment variable, so we must configure JAVA_HOME correctly before starting Tomcat.
b. If the startup fails, prompting that the port number is occupied, then modify the default port 8080 to other unused values, such as 8989 and so on.
[Method] Open: Unzip the directory \conf\server.xml, find the first Connector label, and modify the port attribute
When the web server starts, it actually listens to a port on the machine. When a client sends a request to the port, the web server will process the request. But if the request is not sent to the port it is listening on, the web server will not respond. For example: Tomcat starts listening on port 8989, and the accessed address is http://localhost:8080 , which cannot be accessed normally.
ps: The above interface appears, indicating that the startup is successful
② Stop the Tomcat server
In the bin directory under the decompression path of the Tomcat software (in the path just now), double-click
双击shutdown.bat或者直接点X
, as shown in the following figure:
3.5 Directory structure of Tomcat server software
D:\developer_tools\apache-tomcat-8.5.27, this directory directly contains Tomcat's bin directory, conf directory, etc. We call it the Tomcat installation directory or root directory .
- bin : This directory stores binary executable files. If it is the installation version, there will be two exe files in this directory: tomcat6.exe and tomcat6w.exe. The former is to start Tomcat under the console, and the latter is to pop up The GUI window starts Tomcat; if it is the decompressed version, there will be startup.bat and shutdown.bat files. startup.bat is used to start Tomcat, but it needs to configure the JAVA_HOME environment variable before starting, and shutdown.bat is used to stop Tomcat;
- conf : This is a very, very important directory. There are four most important files in this directory:
- server.xml: Configure the entire server information. For example, modify the port number. The port number of the default HTTP request is: 8080
- tomcat-users.xml: the file for storing tomcat users, where the user name and password of tomcat and the role information of the user are saved. You can add tomcat users according to the comment information in the file, and then you can enter the Tomcat Manager page on the Tomcat home page;
- web.xml: Deployment descriptor file, many MIME types are registered in this file, that is, document types. These MIME types are used to describe the document type between the client and the server. If the user requests an html web page, the server will also tell the client browser that the document responded by the browser is of text/html type, which is a MIME 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 with an exe file, it is impossible for the browser to display it, but a download window should pop up instead. MIME is used to describe what type of content the document is!
- context.xml: Unified configuration for all applications, usually we don't configure it.
- 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 application depends on in this directory. All jar files in this directory can be shared, but in this way your application When placed under other Tomcats, the jar packages in this directory can no longer be shared, so it is recommended to only put the jar packages required by Tomcat in this directory;
- logs : This directory is full of log files, which record information about Tomcat startup and shutdown. If there is an error when starting Tomcat, the exception will also be recorded in the log file.
- temp: store the temporary files of Tomcat, the things in this directory can be deleted after stopping Tomcat!
- webapps: directory for storing web projects, each of which is a project ; if there are already directories in this directory, they are all projects that come with tomcat. Among them, ROOT is a special project. Access it in the address bar: http://127.0.0.1:8080. When the project directory is not given, it corresponds to the ROOT project. http://localhost:8080/examples, enter the example project. Among them, examples is the project name, that is, the name of the folder.
- work : The files generated at runtime, and the final running files are all here . Generated via projects in webapps! You can delete the contents of this directory, and the work directory will be generated again when you run it again. When a client user accesses a JSP file, Tomcat will generate a Java file through JSP, and then compile the Java file to generate a class file, and the generated java and class files will be stored in this directory.
- LICENSE : the license.
- NOTICE: Documentation.
4. How to deploy web projects locally?
practice
①Close the Tomcat server
② Copy and paste the entire folder of the web project to be deployed directly into the webapps directory under the Tomcat decompression path, as shown in the following figure:
③Restart the Tomcat server, as shown in the following figure:
④Type in the browser
http://localhost:8080/要部署的web项目名称/index.html所在目录/index.html
Notice:
If you visit the index.html in the web project you want to deploy in the browser and report a 404 error, it is likely that the name of the web project is wrongly written, or the correct directory of index.html in the project is not found. As follows: