Introduction
Tomcat is a web application server, which is used to deploy web projects and is a lightweight application server. In learning and life, it is very useful. Let me explain to you the download, installation and related configuration of tomcat. Here, tomcat9 is used as an example.
1. Download
Link: tomcat
select tomcat9
and select the version with the suffix tar.gz (you can also choose other versions like tomcat7 or 8, but the suffix must be the tar.gz version.)
Download and decompress to get the following folder:
- bin mainly stores binary executable files and scripts.
- conf mainly stores various configuration files.
- lib is mainly used to store the jar packages that Tomcat needs to load when running.
- logs Mainly store the log files generated by Tomcat during operation.
- temp mainly stores temporary files generated by Tomcat during operation.
- webapps mainly stores applications, and when Tomcat starts, it will load the applications in this directory.
- work mainly stores the compiled files of tomcat at runtime, such as JSP compiled files.
2. Tomcat startup and shutdown
The first method: double-click the startup.bat file in the bin directory when starting,
and enter localhost:8080 in the browser. The following interface appears, indicating that the startup is successful. You can also see the words start[***] appearing in the last line of the tomcat window. It also means that the startup is successful.
In the same way, close tomcat, double-click the shutdown.bat file in the bin directory.
The second method: open the cmd command line window, and cd the bin folder location of the folder where tomcat is located.
cd D:\\apache-tomcat-9.0.68\apache-tomcat-9.0.68\bin
Enter startup.bat to start, shutdown.bat to close
开启
startup.bat
关闭
shutdown.bat
In the linux system, the terminal enters the bin directory and enters ./startup.sh (usually tomcat is configured on the server)
开启
./startup.sh
关闭
./shutdown.sh
3. Precautions
Before starting tomcat, first install JDK and configure the environment variable JAVA_HOME. If JDK is not installed or JAVA_HOME is not configured, it will crash when starting. For details, please refer to the link: JDK installation and environment variable configuration (Win10 detailed version)
4. Related configuration
(1) Garbled character settings
console garbled
Go to the conf directory and open logging.properties, modify UTF-8 to GBK
(2) The web page is garbled
Open the catalina.bat file in the bin directory, and add the following code below @echo off at the top
set "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS% -Dfile.encoding=UTF-8"
As shown below:
port change
The default port of tomcat is 8080, and the address on the web is localhost:8080.
You can see that the IP address of some websites does not have a port number on the browser. Yes, there is no need to enter ":80", so here we also change the port number of tomcat to 80 (for the sake of beauty)
open the server.xml file in the conf directory, and modify the port in the connector tag to 80 (closed in tomcat Modify in the state, if it is turned on, turn it off first)
After the modification, start tomcat and you can see
5. Environment configuration
Every time it is troublesome to start tomcat in the control line cd directory, you can directly enter startup.bat to configure environment variables.
Take Windows as an example, configure environment variables:
Then open the command line and enter startup, and the configuration is successful
if the command line displays the error message as follows
The CATALINA_HOME environment variable is not defined correctly This environment variable is needed to run this program
Check whether the environment variable name is written correctly! ! The name of CALALINA_HOME must be entered correctly. If you write less or misspelled letters, an error will be reported. If there is no problem with the input, the environment variables of tomcat have been configured before, and the residual information should be deleted.
6. Project deployment
As mentioned above, the project to be deployed is placed under the webapps folder, so we create a folder hello, create a new index.html file in it, and write Hello World! Start tomcat and enter the address, for example: localhost/hello/
index.html , as shown in the figure below, it means that the deployment is successful.
Of course, we can’t create files and pages in such a rough way in actual development. Specific projects still need to be configured specifically. For example, small projects like jsp+servlet place the entire project file Under the webapps folder, the ssm project is to put the jar package or war package after the project is packaged. If it is a springboot project, it is another matter, because the springboot project has built-in tomcat, you only need to configure it in the yml or properties file The start port of tomcat will do.
In addition, there is a little virtual path. If we want to remove the hello in the above address localhost/hello/index.html to be the project name, then we need to write the context configuration in the first Host tag of server.xml.
<Context docBase="hello" path="/" reloadable="true"/>
Then start tomcat (also turn tomcat off and on now), and the browser will see the following picture:
In this way, you can directly access the index page.
conclusion
The above is the entire content of this article. If there is something wrong, please point it out and let's exchange and learn together.