Tomcat server


1、 Server related concepts
1. What is a server

Server: divided into software server and hardware server
Hardware server: a computer running on the Internet with static IP (usually with high configuration)
Software server: a computer program (software) running on the Internet. Only when the server software is installed on the hardware server, can it provide external services.
    There are many kinds of server software: database server (mysql, Oracle, SQL server, etc.), web server (tomcat, jetty, JBoss, etc.), mail server, FTP server...

2. What web server

Web server: a computer program running on the Internet, which is specially used to receive the client's request, process it according to the request, and finally give a response!
For example: open the browser, input“ "Enter, in fact, what you visit is Baidu's server. At this time, you will send a request to Baidu server, asking for Baidu's home page. Baidu server will receive and process the request, and give a response according to the request (respond Baidu's home page to the client browser)

Tomcat is a web server, which is small and flexible, easy to use, low learning cost!

2、 Download, install, start and configure Tomcat server
1. Download Tomcat server

Download address:
Tomcat is divided into many versions, including Windows Version (decompression version and installation version) and Linux version
It is recommended to use the decompressed Version (decompress when you need to use it, and delete the decompressed directory directly if you don't need to use it!)

2. Installation, startup and configuration of Tomcat
2.1. Install Tomcat

Installation: it can be used after decompression (it is better not to include Chinese and spaces in the installation path)

Before starting tomcat, you need to configure (check) a Java_ Home environment variable, which needs to point to the installation root directory of JDK
Variable name: Java_ HOME
Variable value: D: \ "software \" Java \ "jdk1.8.0_ one hundred and sixty-one
Since Tomcat server is developed by java language, running Tomcat needs the support of JDK
JAVA_ The variable home is telling the Tomcat server which location of JDK to use

2.1. Start Tomcat

Start Tomcat: the Tomcat server can be started through the [Tomcat installation directory] / bin / startup.bat file;
Shut down Tomcat: the Tomcat server can be shut down through the [Tomcat installation directory] / bin / shutdown.bat file;
        Or just click the cross in the upper right corner
After starting tomcat, you can open the browser and visit: 
If you can visit the home page of Tomcat server, Tomcat is installed and started successfully!

3. Modify the default port of Tomcat server

If you do not modify the port, you need to add 8080 after the host name / IP address every time you visit the Tomcat server
If you want to omit the port after the host name or IP address when accessing, you can modify the port to 80 (this port is special, you can omit it and do not write it!)

The method to modify the port is as follows:
    Find [Tomcat installation directory] / conf / server. XML and open the file with the text editing tool
    Find line 69 of the file, change the port attribute value on the connector label to 80, save the file, and restart the server to take effect!
After restarting the server, you can access the Tomcat server through the following path:

3、 Directory structure of Tomcat server

Bin: directory where batch files are stored (startup.bat, shutdown.bat files)
Conf: the directory where Tomcat configuration files are stored (server.xml is the Tomcat core configuration file)
Lib: the directory that stores the jar packages that the Tomcat server depends on at runtime
Logs: the directory where the log files generated by the Tomcat server at run time exist
Temp: the directory to store the temporary files generated by Tomcat server at runtime

Work: store some work files generated by Tomcat server during operation
    (the translated servlet file, the
    Session object serialization (serialization refers to: transferring objects in the form of stream on the Internet or saving them on the hard disk; Deserialization: read the file into the server's memory in the form of stream and turn it into an object). All the generated files will be placed in this directory.)
Webapps: it is the directory where web applications are stored,
    It can be accessed through the localhost virtual host
    The webapps directory is the default directory for the localhost host to store web applications
    Putting the web application in the webapps directory is equivalent to publishing it to the localhost

4、 Web application and virtual host
1. Web application:

Web application is actually a directory, which can contain many resource files (HTML / CSS / JS / picture / JSP / servlet, etc.)
Web resource files (HTML / CSS / JS / picture / JSP, etc.) cannot be managed directly in virtual host
Web resource files need to be organized into a web application (directory), and web applications can only be managed by the virtual host if they are published and run in the virtual host

2. Virtual host:

It is a site configured in Tomcat server. When accessing, it is like accessing a real independent host
We call this site a virtual host running on a Tomcat server
Tomcat server can be configured with multiple sites, each site is a virtual host.
The following is the configuration of localhost provided by Tomcat by default:
<Host name="localhost" appBase="webapps"...></Host>
<Host name="" appBase="baidu"...></Host>
1) A Tomcat server software is installed on the server hardware
2) Multiple sites (virtual hosts) can be configured inside the Tomcat server software, and Tomcat comes with a localhost virtual host by default.
3) Webapps is the default directory of web applications managed by the localhost virtual host. Web applications published in the webapps directory are also published in the localhost
4) A JT web application is published to webapps, which contains some web resource files
5) Web resource files can be (HTML / CSS / JS / image / servlet / JSP, etc.)

5、 Web application
1. Directory structure of web application

News (directory, web application)
 |--You can also put the web resource file in the root directory of the web application
 |--Other directories (resource files placed in other directories can be directly accessed by the browser)
 |--WEB-INF directory (private directory, the resource files in it can't be directly accessed by the browser)
         |--Classes directory (the compiled class files of Java programs will be placed in this directory)
         |--Lib directory (the jar packages that web applications rely on will be placed in this directory)
         |--Web.xml file (the core configuration file of current web application)

2. How to publish a web application to virtual host

Directly copy the web application directory to the directory managed by the virtual host
For example, copy news to the webapps directory, because webapps is the directory where the localhost hosts publish web applications,
Therefore, it is equivalent to publishing news to the localhost, which can be accessed through the localhost!

Tomcat server

3. Extended content: how to configure a virtual host

Tomcat server provides a virtual host called localhost

1) In the [Tomcat installation directory] / conf / server.xml file, find the host tag (more than 140 lines), above or below this tag

Add a host tag as follows:

<Host name="" appBase="baidu123"></Host>
The name property specifies the host name
The appbase attribute is used to specify the directory where the host publishes the web application

2) Simulate the mapping relationship between host name and IP address in DNS server

Find the hosts file: C / Windows / system32 / Drivers / etc / hosts
There are some comments in this file (which can be ignored). Add the following configuration in the first line:

3) Restart the server. When the server reads the host tag we added, it will help us create the baidu 123 directory in the Tomcat root directory

Then publish a web application (such as news) to the baidu 123 directory and access it through the following path: