Tomcat+Maven+Servlet installation and deployment


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
insert image description here
(2) Get a compressed package
insert image description here
(3) Just decompress it directly
insert image description here
(4) Test whether it is successful
insert image description here

insert image description here
insert image description here
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.
insert image description here


Two, Maven+Servlet deployment

1. Chuangjian Maven project (idea2021community)

(1) First create a Maven project
insert image description here
insert image description here

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
insert image description 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.
insert image description here
insert image description here

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.
insert image description here
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.
insert image description here

5. Manually package the web project

(1) Configure the packaged type and package name in pom.xml.
insert image description here
(2) Click Maven on the right, find the package and double-click it.
insert image description here
The packaging is successful as shown below:
insert image description here
insert image description here

6. Test in browser

(1) Start
the startup.bat in the Tomcat bin directory.
insert image description here
(2) Put the packaged .war package under webapps
insert image description here
(3) Construct an HTTP request in the browser
Access different classes in the same package according to the class name configured in @WebServlet.
insert image description here


insert image description here

Guess you like

Origin blog.csdn.net/qq_45283185/article/details/127768157