Article Directory
foreword
Tomcat is an open source, free and widely used HTTP server based on Java. Tomcat is an HTTP server, essentially a TCP server, but some codes that are parsed/constructed according to the HTTP protocol format are added on top of it.
1. Download and install Tomcat8
(1) Official website download address: https://tomcat.apache.org/download-80.cgi
(2) Get a compressed package
(3) Just decompress it directly
(4) Test whether it is successful
The garbled characters here are because the cmd of Windows 10 defaults to gbk encoding while Tomcat uses utf-8 encoding, so it doesn’t matter if you don’t process it.
Two, Maven+Servlet deployment
1. Chuangjian Maven project (idea2021community)
(1) First create a Maven project
2. Introduce servlet dependency under pom.xml
(1) You can search the servlet in the MVN central warehouse to import the corresponding version of the servlet dependency. I am using Tomcat8+servlet3.1.0 here
(2) Click the refresh button in Maven to download it. The initial introduction time will be longer, you can change it A better network or configuration of domestic sources.
3. Create webapp/WEB-INF/web.xml under main
Because Tomcat's webapps can run multiple web projects, we deploy a web project under webapps. web.xml is mainly used for verification and completion. Fill it with the following content. Even if an error is reported, it will not affect the project. You can press alt+enter to download or ignore it.
web.xml (example):
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
4. Verify whether HttpServlet is imported (configure @WebServlet path)
Write a piece of code to verify whether HttpServlet is installed.
Be sure to remember the name of the configuration class here, you will need to use it to access your class when constructing an HTTP request in the browser later.
5. Manually package the web project
(1) Configure the packaged type and package name in pom.xml.
(2) Click Maven on the right, find the package and double-click it.
The packaging is successful as shown below:
6. Test in browser
(1) Start
the startup.bat in the Tomcat bin directory.
(2) Put the packaged .war package under webapps
(3) Construct an HTTP request in the browser
Access different classes in the same package according to the class name configured in @WebServlet.