Servlet web container in springboot

Time:2022-1-13

1. Preface

Springboot supports embeddedServletContainer:
Servlet web container in springboot

SpringBoot 2.0.3. Release requires Java 8 or 9 and spring framework 5.0.7 Release or later. Provides explicit build support for Maven 3.2 + and gradle 4.

2. Use Tomcat

Springboot defaultServletContainer isTomcat, no additional configuration is required, just importspring-boot-starter-webDependence.

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
</dependencies>

Start the application and run the screenshot as follows:
Servlet web container in springboot

3. Use jetty

Spring boot switches the default Tomcat to jetty. The steps are as follows.

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
</dependencies>

Start the application and run the screenshot as follows:
Servlet web container in springboot

3.1. Why use jetty

Google App Engine abandoned Tomcat and chose jetty. This is mainly because jetty is smaller and more flexible than Tomcat. Server size in the cloud is very important because thousands of jetty instances may run in 10 seconds. If each server can save 1m, GB of memory will be saved in 10 seconds.

4. Use undertow

Spring boot switches the default Tomcat to undertow. The steps are as follows.

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
</dependencies>

Start the application and run the screenshot as follows:

Servlet web container in springboot

This work adoptsCC agreement, reprint must indicate the author and the link to this article