Build a project with maven and configure tomcat to run a servlet

1. Use idea to create an empty maven project and delete the src folder in it, because in the later stage, each module will be established under the new project, and the original src folder is not required.
2. Under the general project, maven is used to introduce the jar package of servlet and jsp, which can be found in( https://mvnrepository.com/ )Search for servlets and JSPS in, find the corresponding jar package, and find the configuration file POM Add the dependencies tag pair to the XML, then copy the following code to the tag pair, and maven will automatically import the two jar packages and their dependent jar packages into the project (if prompted in the lower right corner of idea, ask whether you want to enable auto import and select auto import).
After successful import, you can see the following in the project:

3. Create a sub module under the project. The sub module is created with webapp template. Pay attention to the relationship between the sub module and the parent module.

  • Create sub module

  • POM in sub module XML is as follows (if there is no pom.xml that can be manually added to the sub module)

<parent>
    <groupId>com.chong</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  • POM of parent module XML has the following parts
 <modules>
        <module>servlet</module>
    </modules>

4. Optimize the web in the template XML, copy and paste the following code into the document

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true"
>
</web-app>


5. Create two folders under main, one named java and the other resources. The java folder is marked as the root directory and the Resources folder is marked as the resource directory.


6. When configuring Tomcat, pay attention to modifying the mapping (such as "/ s1" modified here), which is more convenient

7. Create a package in java, then create a servlet in your own package, rewrite the doget and dopost methods, and write some information for testing.
About rewriting: after creating a class and inheriting HttpServlet, press the shortcut key alt+insert in idea, and then a dialog box pops up. Select overwirtter, and then select doGet and doPost methods to rewrite. The codes in this class are as follows:

package com.chong.day34;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Called doGet method!");
        PrintWriter writer = resp.getWriter();
        writer.print("HelloServlet!");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

8. In the previously modified web Register the servlet in the XML configuration file, that is, add two tag pairs and related information, as shown in the figure

The servlet name should be the same in both tag pairs. URL pattern is the configured access path, and servlet class is the servlet class

9. Start Tomcat and test

Then enter hello after s1 / to access the servlet just written

Keywords: Java Maven Tomcat jar

Added by Reformed on Sat, 05 Mar 2022 07:37:27 +0200