目录
一、封装类解读
二、注册Servlet三大组件(Servlet、Filter、Listener)
自定义这三个组件
WebConfig
MyFilter
MyListener
HelloController
hello1.html
hello2.html
三、使用外置的Servlet容器
1.一定要确保打包方式是war包
2.将嵌入式的Tomcat指定为provided
3.配置项目的目录结构
编辑 4. 部署Tomcat
在前面的文章中也提到了,SpringBoot项目和SSM架构的区别之一就是免除了自己配置外置服务器(里面内嵌了服务器,如Tomcat、Jetty等),但是默认配置在某些情况下并不能满足所有的业务场景,这就需要开发者配置嵌入式服务器。
而在 Java Web 应用开发领域,Tomcat仍旧是一个大项,所以今天就以为SpringBoot项目配置Tomcat为例。
一、封装类解读
依旧是老规矩,讲解这板块内容前先来看一下SpringBoot对这部分的内置封装类:
SpringBoot中封装了内嵌服务器的配置信息的配置类是ServerProperties,
包括端口、上下文路径、SSL配置、线程池配置等。

这里面的默认属性都可以通过application.properties或application.yml进行重新配置,主要有这些属性可以修改(这里以application.properties为例):
# 设置内嵌服务器监听的端口号,默认为8080
server.port=8081# 设置服务器绑定的IP地址,默认绑定所有可用地址
server.address=127.0.0.1# 设置应用的上下文路径,访问应用时需要包含该路径
server.servlet.context-path=/myapp# 设置会话超时时间,可以使用时间单位(如s、m、h)
server.servlet.session.timeout=30m# 自定义会话Cookie的名称
server.servlet.session.cookie.name=MY_SESSION_ID
# 自定义会话Cookie的域名
server.servlet.session.cookie.domain=example.com
# 自定义会话Cookie的路径
server.servlet.session.cookie.path=/
# 自定义会话Cookie的最大存活时间(秒)
server.servlet.session.cookie.max-age=3600
# 设置会话Cookie是否仅通过HTTPS传输
server.servlet.session.cookie.secure=true
# 设置会话Cookie是否仅由HTTP访问
server.servlet.session.cookie.http-only=true# 启用SSL支持
server.ssl.enabled=true
# 配置SSL证书文件路径(相对路径或绝对路径)
server.ssl.key-store=classpath:keystore.jks
# 配置SSL证书文件密码
server.ssl.key-store-password=secret
# 配置SSL证书别名
server.ssl.key-alias=tomcat
# 配置SSL证书类型(如JKS、PKCS12)
server.ssl.key-store-type=JKS
# 配置SSL协议(如TLS)
server.ssl.protocol=TLS# 启用响应压缩
server.compression.enabled=true
# 设置响应压缩的最小响应大小(单位为字节)
server.compression.min-response-size=1024
# 设置需要压缩的MIME类型
server.compression.mime-types=text/html,text/xml,text/plain,application/json# 设置错误处理路径
server.error.path=/error
# 设置错误处理的include-stacktrace属性,控制是否在错误页面显示堆栈跟踪
server.error.include-stacktrace=on_trace_param
二、注册Servlet三大组件(Servlet、Filter、Listener)
在Spring Boot项目中,以下类用于封装和注册Servlet、Filter和Listener(这里不再演示里面具体的源码,可以自行在idea搜索引擎中搜索查看):
-
ServletRegistrationBean
:用于注册Servlet。 -
FilterRegistrationBean
:用于注册Filter。 -
ServletListenerRegistrationBean
:用于注册Listener。 -
ServletWebServerFactoryCustomizer
:用于自定义嵌入式Servlet容器的配置。
解释一下这三个组件的主要功能:
- Servlet:主要负责接收客户端请求,执行业务逻辑并生成响应返回给客户端,是处理业务逻辑的核心。
- Filter:用于对请求和响应进行拦截处理,可进行请求预处理、响应后处理以及日志记录和监控等操作。
- Listener:监听 Servlet 容器中的各种事件,如 ServletContext、HttpSession 和 ServletRequest 等对象的生命周期事件,以便在事件发生时执行相应逻辑。
开发者可以通过 @Bean
注解在Spring Boot项目中自定义和注册Servlet、Filter和Listener。这种方式允许开发者以编程的方式配置这些组件,而不是依赖于传统的 web.xml
配置文件。
【在传统的SSM(Spring + SpringMVC + MyBatis)项目中,Servlet、Filter和Listener的注册通常需要在 web.xml
文件中进行配置】
自定义这三个组件

WebConfig
package com.qcby.sbservlet.config;import com.qcby.sbservlet.filter.MyFilter;
import com.qcby.sbservlet.listener.MyListener;
import com.qcby.sbservlet.servlet.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;@Configuration
public class WebConfig {//注册三大组件@Beanpublic ServletRegistrationBean myServlet(){ServletRegistrationBean registrationBean = new ServletRegistrationBean(newMyServlet(),"/myServlet");return registrationBean;}@Beanpublic FilterRegistrationBean myFilter(){FilterRegistrationBean registrationBean = new FilterRegistrationBean();registrationBean.setFilter(new MyFilter());registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));return registrationBean;}@Beanpublic ServletListenerRegistrationBean myListener(){ServletListenerRegistrationBean<MyListener> registrationBean = newServletListenerRegistrationBean<>(new MyListener());return registrationBean;}
}
MyFilter
package com.qcby.sbservlet.filter;import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;@WebFilter("/hello")
public class MyFilter implements Filter {@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {System.out.println("Filter processing...");chain.doFilter(request, response);}
}
MyListener
package com.qcby.sbservlet.listener;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;@WebListener
public class MyListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("ServletContext initialized");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {System.out.println("ServletContext destroyed");}
}
HelloController
package com.qcby.sbservlet.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@GetMapping("/hello1")public String hello() {return "Hello, World 1!";}@GetMapping("/hello2")public String hello2() {return "Hello, World 2!";}
}
hello1.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Hello1</title>
</head>
<body>
<h1>Hello, World 11!</h1>
</body>
</html>
hello2.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Hello2</title>
</head>
<body>
<h1>Hello, World 22!</h1>
</body>
</html>
三、使用外置的Servlet容器
配置一个项目参见下面这篇博客:
SpringBoot(一)--搭建架构5种方法_springboot框架搭建-CSDN博客文章浏览阅读2.2k次,点赞19次,收藏39次。Spring Boot 是基于 Spring 框架,以约定优于配置、自动配置为核心,可快速构建独立运行的应用,为微服务等开发提供便利的开发框架。前面已经对SSM(Spring,SpringMVC,MyBatis)每个框架做了讲解,SpringBoot就是基于这个框架一个更简单、更有利于开发。_springboot框架搭建
https://blog.csdn.net/m0_74977981/article/details/146139845?spm=1001.2014.3001.5501注意在创建时的打包方式选择war包,创建时更加推荐用创建用maven项目改成SpringBoot项目的创建方式,但是直接创建一个SpringBoot操作起来更加简单,因为里面很多配置都自动集成了。
1.一定要确保打包方式是war包
在使用外置容器部署Java Web应用时,WAR文件是标准的打包格式,具有以下优势:
符合Java EE规范,确保通用性和兼容性。
目录结构清晰,易于管理和部署。
支持多种部署方式,包括热部署。
适用于传统Java Web应用和Spring Boot应用的外置容器部署场景。
因此,为了确保应用能够顺利部署到外置容器中,通常需要将应用打包为WAR文件。
2.将嵌入式的Tomcat指定为provided
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope>
</dependency>
3.配置项目的目录结构
配置:
得到这样:
4. 部署Tomcat


选择第二个:
点击应用:
必须编写一个SpringBootServletInitializer的子类,并调用configure方法:
package com.qcby.outtomcat;import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;public class ServletInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(OuttomcatApplication.class);}}
不要忘了配置文件:
搭建完毕,启动服务器就可以使用。
