跳到主要内容

Spring Boot 3.x- Servlet Web应用程序开发(嵌入式容器)

系列文章目录

系列文章:Spring Boot 3.x 系列教程


文章目录

  • 系列文章目录
  • 前言
  • 使用非Tomcat容器
  • Servlet, Filter, 和 Listener
    • 注册
  • 扫描
  • Servlet上下文初始化
  • ServletWebServerApplicationContext
  • 定制嵌入式Servlet容器
  • SameSite Cookie
  • 编码配置容器
  • 直接定制ConfigurableServletWebServerFactory
  • JSP限制

前言

对于servlet应用程序,Spring Boot包括对嵌入式TomcatJettyUndertow服务器的支持。大多数开发人员使用适当的“Starter”来获得完全配置的实例。默认情况下,嵌入式服务器在端口8080上侦听HTTP请求。

使用非Tomcat容器

许多Spring Boot启动器都包含默认的嵌入式容器。对于servlet堆栈应用程序,spring-boot-starter-web通过包含spring-boot-starter-tomcat来包含Tomcat,但您可以使用spring-boot-starter-jettyspring-boot-starter-undertow来代替。

当切换到不同的HTTP服务器时,您需要将默认依赖项交换为您需要的依赖项。为了帮助完成这个过程,Spring Boot为每个受支持的HTTP服务器提供了一个单独的starter。

下面的Maven示例展示了如何在Spring MVC中排除Tomcat并包含Jetty:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Use Jetty instead -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>