“This is my 38th day of participating in the First Challenge 2022. For details: First Challenge 2022”

1. The introduction

As a Java developer, Tomcat knows everything. Is this interface familiar?

Previously, Tomcat was deployed separately and the service was packaged into a WAR package for deployment. With the development of technology, the front and back ends are separated. Tomcat is no longer required to deploy front-end pages. So Tomcat alone is not a good fit. Those of you who are developing web applications using the Spring Boot framework will probably notice that we are not deploying Tomcat but simply running a method front end to access the interface we are developing. Why is that? This is why we can see some information about Tomcat in our Spring Boot Web application

2. What exactly is embedded Tomcat?

It is essentially Tomcat, but instead of deploying a WAR separately and then deploying it in a specified directory, embedding can be embedded into a developer’s project as a Jar package. This is why Spring Boot Web projects can use Tomcat. Embedded Tomcat integrates the previously traditional Tomcat code into a developer’s Web application. Eliminate cumbersome configuration. This also happens to coincide with the idea of Spring Boot.

3. Create an embedded Tomcat application

Tips: Tomcat version 9.0.58, the higher version requires the introduction of some packages from Jakarta EE, as in the Maven project

3.1 create pom. XML

Add dependencies to the POM.xml file

<properties>
    <tomcat.version>9.0.58</tomcat.version>
</properties>

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>${tomcat.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <version>${tomcat.version}</version>
</dependency>
Copy the code

3.2 Writing startup programs

public class TomcatBootstrap {

    public static void main(String[] args) throws Exception{
        // The first part
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);
        tomcat.getConnector();

        // Part 2
        Context ctx = tomcat.addWebapp("".new File("java-sample/src/main/webapp").getAbsolutePath());
        WebResourceRoot resources = new StandardRoot(ctx);
        resources.addPreResources(
            new DirResourceSet(resources, "/WEB-INF/classes".new File("java-sample/target/classes").getAbsolutePath(), "/"));
        ctx.setResources(resources);

        // Part 3tomcat.start(); tomcat.getServer().await(); }}@WebServlet(urlPatterns = "/")
public class MxsmServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("hello world"); }}Copy the code

Tips: The code above may be marked differently depending on the person’s code structure. You can adjust according to error prompts.

Then start the service as shown below:

3.3 Verification Procedure

Enter http://localhost:8080/ in your browser

Hello World is returned, indicating that Tomcat is running properly.

4. To summarize

  • Embedded Tomcat provides a programmatic way to integrate Tomcat into our project, allowing Web projects to run as Jar packages
  • Configuring Tomcat programmatically instead of configuration files reduces the number of configuration files. This is also intended to support annotations for servlets. Anything that used to require XML configuration can now be done with annotations

I am ant back elephant, the article is helpful to you like to pay attention to me, the article has incorrect place please give correct comments ~ thank you