Learn about Tomcat in one article

1. Introduction to Tomcat

What is Tomcat

​ Tomcat is a core project of the Apache Software Foundation. It is an open source and free lightweight web server that supports a small amount of Servlet/jspJavaEE规范. Tomcat is also called a Web container, Servlet container.

Official website: https://tomcat.apache.org/

image-20231216182326945

What is JavaEE

JavaEE: Java Enterprise Edition, Java Enterprise Edition. Refers to the sum of technical specifications for Java enterprise-level development. Contains 13 technical specifications: JDBC, JNDI, EJB, RMI, JSP, Servlet, XML, JMS, Java IDL, JTS, JTA, JavaMail, JAF

2. Tomcat usage configuration

2.1. Tomcat download and start

Tomcat download and installation

  1. Download: https://tomcat.apache.org/
  2. Installation: Tomcat is a green version, just decompress it directly. Suggestion: Do not have a Chinese directory, and the directory level should not be too deep.
  3. Openapache-tomcat directory and you will see the following directory structure. You need to understand the content contained in each directory

Tomcat starts and shuts down

  • Startup: Double-click: bin\startup.bat

image-20231216184037664

  • closure
    1. Directly x out the running window: force close
    2. bin\shutdown.bat: normal shutdown
    3. ctrl+c: Normal shutdown

image-20231216184311455

Tomcat access

​ Access method: browser inputlocalhost:8080, Tomcat default port is 8080

image-20231216184524718

2.2. Tomcat startup garbled code

question

image-20231216185026002

The console has Chinese garbled characters, and conf/logging.properties needs to be modified.

  1. Enter Tomcat's conf directory
  2. Find the logging.properties file
  3. Replace all UTF-8 inside with GBK

image-20231216185712089

注意:It is recommended to use Vscode to open or other tools to directly search and replace to avoid errors.

2.3. Tomcat port number modification
  1. Enter the conf directory of Tomcat
  2. Find the server.xml file and open it
  3. Find the following code location, probably on line 69, and modify port = the location you want.
<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />

image-20231216204107779

  • Port number conflict

image-20231216204046870

3. Tomcat project deployment

Two methods of project deployment

  1. Copy the project directly to the webapps directory
  2. Use compressed file .war method
    • Pack the entire project into a zip file using a compression tool
    • Change the zip extension to war
    • Copy it to the webapps directory and tomcat will automatically decompress it into a directory with the same name.

注意:The files inside cannot have Chinese names

Deployment demonstration: copy the project directly to the webapps directory

  • The project name is: hello, copy its files to the webapps directory

  • After starting Tomcat, visit hello

image-20231216205645843

Deployment demonstration: compressed file .war

  1. Compress project into zip file
  2. Change the zip extension to war
  3. Copy it to the webapps directory and tomcat will automatically decompress it into a directory with the same name.

image-20231216210212334

  • access test

image-20231216210457038

4. Using Tomcat in IDEA

  • The project can be packaged into a war package through Maven's package command, copy the war file to Tomcat's webapps directory, start Tomcat to deploy the project successfully, and then access it through a browser.
  • However, during the development process, the content of the project will often change. It is very inconvenient to deploy tests in the above way.
  • How to quickly use Tomcat in IDEA?

Integrate local Tomcat into IDEA

  • Open the panel to add local Tomcat

image-20231216213158756

  • Specify local Tomcat

image-20231216213333077

  • Configure local Tomcat

image-20231216213728385

Deploy the project into integrated Tomcat

image-20231216214050685

image-20231216214439435

Extended content: What is the difference between the two deployment project modes of xxx.war and xxx.war exploded?

  • The war mode is to convert the WEB project into a war package and publish the war package to the Tomcat server.

  • The war exploded mode publishes the WEB project to the Tomcat server in the location of the current folder.

  • After the war mode deployment is successful, there will be deployed project content in Tomcat's webapps directory.

  • After the war exploded mode is successfully deployed, there is no webapps directory in Tomcat, but the content in the target directory of the project is used for deployment.

  • It is recommended that everyone choose war mode for deployment, which is more in line with the actual situation of project deployment.

Guess you like

Origin blog.csdn.net/weixin_53961667/article/details/135038992