After getting used to using SpringBoot, I almost forgot what Tomcat is. This article will not cover those theoretical classes, but directly look at how to install and use Tomcat.

 

First, version selection

First we need to choose the right version, you can refer to the official release notes: tomcat.apache.org/whichversio…

Here, I’m using Java8 more often, and I don’t like the latest version (possibly unstable or with some untrodden potholes), so I choose Tomcat9

 

 

Second, the download

Tomcat is a free, open source web server software, so we can directly on the website to download: tomcat.apache.org/index.html

Select the version you want to download on the left

Here I selected Tomcat 9, directly download the latest release, the bottom of the page is the latest version 9.0.43, download the binary core version, do not download the source version, it is only used to study the source code, can not be directly implemented.

Select the 64-bit zip package to download directly:

If you want to download other versions:

If you don’t want to download the latest release, click Archives on the page and all versions of 9 will be available for download

The version list is shown below

Click the version you want to download, go to the picture, click bin, SRC is the source package if you want to study the source code can be downloaded, but if you want to use the package must be selected under bin

Windows generally still choose the 64-bit package, some people like to use the executable file installation, but I personally recommend the compressed package format, easy to manage

 

 

Three, installation,

The installation method of compressed package is very simple, directly decompress to the place you want to put

Since Tomcat is written in Java, JDK environment support is required. JDK environment variables must be configured on the local computer.

But I don’t think most people need to worry about this, since you’re running Java Web projects using Tomcat and you don’t even have a JDK environment.

 

Four, start,

To start the tomcat, run the bin/startup.bat file in the Tomcat installation directory.

If a black box is displayed, it is successful (see troubleshooting for garbled characters).

To test, open a browser and type http://localhost:8080/ in the address bar

If the following interface appears, the startup is successful

 

5. Local configuration

From the above, we have started Tomcat normally, but there are some configurations that can be optimized for use.

In my opinion, sometimes this step is not necessary. Most of the scripts are directly launched by double-clicking or integrated into IDEA and Eclipse, which can be used normally without setting environment variables.

But if you have time, you can also browse through the configuration of one, I have written the tutorial. After all, there is no guarantee that one day when you integrate Tomcat into a new development tool, you will be required to configure Tomcat’s environment variables.

1. Why do I need to configure environment variables

Tomcat was started by double-clicking the startup.bat script, but it can also be started by running the CMD command:

Bat. One is to control the current directory to the bin directory of Tomcat and directly execute startup.bat. This method does not need to configure any environment variables and is the same as manually double-click the startup.

Bat script in the tomcat directory. In this case, any file can be executed, as long as the file does not depend on other files. The startup.bat script relies on a script called Catalina.bat in the bin directory as well. So executing startup.bat in this way will result in an error, as shown in the following figure:

In this case, we need to configure the CATALINA_HOME system environment variable so that it can find the bin\ Catalina.bat script

2. Configure environment variables

If you don’t have JDK environment variables, you can’t start Tomcat.

Create a new environment variable directly under the system variable:

Variable name: CATALINA_HOME

Variable value: Tomcat installation directory

This solves the second boot mode mentioned above.

Bat script to start Tomcat by typing startup.bat in any CMD directory.

Add a new Path to the system variable:

The CATALINA_HOME value is: % % \ bin

I’m going to be lazy here and just use the CATALINA_HOME environment variable, or you can create a new TOMCAT_HOME variable, and it’s going to have the same value as CATALINA_HOME, and you can add it in, whatever.

Testing:

If you type startup.bat anywhere, it will start normally.

3. Register in the service list

We can also register it in the Windows service list, so that you can choose to configure the boot, background boot, etc. After all, tomcat has to have a command line window every time you open it.

The principle is the service. Bat script in the tomcat directory. Make sure you have this script, and the environment variables are configured in front of you, we can directly register

Enter commands anywhere in CMD:

service install
Copy the code

As shown, the registration was successful in an instant

To uninstall the service, enter

service remove
Copy the code

Can be

The service.bat script in the tomcat\bin directory is essentially executed.

Testing:

Open up the list of services, and you can see that it already exists here.

 

6. Problem solving

1. Logs are garbled characters

Here we will find a lot of log garble in the window, due to the encoding format. Windows command line coding and the system default consistent, we are China is naturally GBK coding. Tomcat’s log encoding is UTF-8, so fix that.

Open the configuration file apache-tomcat-9.0.43\conf\logging.properties in the tomcat directory

Locate the configuration line below

java.util.logging.ConsoleHandler.encoding = UTF-8
Copy the code

Change utF-8 to GBK, restart, and you will find that it is normal