Spring Boot嵌入式的Servlet容器
一.查看SpringBoot默认的嵌入式Servlet容器(默认使用的是tomcat)
在IDEA的项目的pom文件中按Ctrl + shift + Alt + U可以打开SpringBoot依赖的图表,如下所示

可以发现默认嵌入式的tomcat服务器的版本为9.0.16
二.修改Servlet容器的默认配置
1.直接在application.properties或yml文件中配置,例如
#通用的Servlet容器设置server.xxx
server.port=
server.context‐path=/demo
server.tomcat.uri‐encoding=UTF‐
#Tomcat的设置server.tomcat.xxx
#server.tomcat.port=8088
2.编写一个EmbeddedServletContainerCustomizer:嵌入式的Servlet容器的定制器;来修改Servlet容器的配置。
三.注册Servlet三大组件(Servlet、Filter、Listener)
由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。
因此
1.创建组件
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("hello Servlet");
}
}
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("过滤器已经执行了");
filterChain.doFilter(servletRequest , servletResponse);
}
}
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("web项目启动了");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("web项目销毁了");
}
}
2.注册这些组件
@Configuration
public class MyMvcConfig implements WebMvcConfigurer { //注册三大组件
@Bean //Servlet组件
public ServletRegistrationBean myServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
return registrationBean;
}
@Bean //Filter组件
public FilterRegistrationBean myFilter(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
registrationBean.setUrlPatterns(Arrays.asList("/myFilter"));
return registrationBean;
}
@Bean //Listener组件
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
return registrationBean;
}
}
3.测试:
启动项目后发现控制台输出

访问 /myServlet 后发现页面输出

访问/myFilter后,控制台输出

项目退出运行后,控制台输出

四.SpringBoot切换其他Servlet容器
Jetty(适合开发长连接的应用):添加依赖如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
<exclusions><!--排除默认的tomcat服务器-->
<exclusion>
<artifactId>spring‐boot‐starter‐tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<artifactId>spring‐boot‐starter‐jetty</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
Undertow(不支持JSP,但是是非阻塞的,并发性能比较好),添加依赖如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
<exclusions><!--排除默认的tomcat服务器-->
<exclusion>
<artifactId>spring‐boot‐starter‐tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<artifactId>spring‐boot‐starter‐undertow</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
Spring Boot嵌入式的Servlet容器的更多相关文章
- (7)Spring Boot web开发 --- servlet容器
文章目录 配置嵌入式 Servlet 容器 注册 三大组件 使用其他 servlet 容器 使用外置的 `Servlet` 容器 配置嵌入式 Servlet 容器 Spirng Boot 默认使用自带 ...
- spring boot项目发布tomcat容器(包含发布到tomcat6的方法)
spring boot因为内嵌tomcat容器,所以可以通过打包为jar包的方法将项目发布,但是如何将spring boot项目打包成可发布到tomcat中的war包项目呢? 1. 既然需要打包成wa ...
- 【串线篇】spring boot嵌入式Servlet容器启动原理;
什么时候创建嵌入式的Servlet容器工厂?什么时候获取嵌入式的Servlet容器并启动Tomcat: 获取嵌入式的Servlet容器工厂: 1).SpringBoot应用启动运行run方法 2).r ...
- 【串线篇】spring boot嵌入式Servlet容器自动配置原理
EmbeddedServletContainerAutoConfiguration:嵌入式的Servlet容器自动配置? @AutoConfigureOrder(Ordered.HIGHEST_PREC ...
- Spring Boot 嵌入式Web容器
目录 前言 1.起源 2.容器启动流程解析 2.1.获取应用类型 2.2.容器启动流程 3.加载 Web 容器工厂 4.总结 前言 最近在学习Spring Boot相关的课程,过程中以 ...
- Spring Boot之注册servlet三大组件
由于Spring Boot默认是以jar包的形式启动嵌入式的Servlet容器来启动Spring Boot的web应用是,没有web.xml配置文件 注册三大组件用以下方式 ServletRegist ...
- spring boot 加载web容器tomcat流程源码分析
spring boot 加载web容器tomcat流程源码分析 我本地的springboot版本是2.5.1,后面的分析都是基于这个版本 <parent> <groupId>o ...
- 18. Spring Boot 、注册Servlet三大组件Servlet、Filter、Listener
由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件 public class MyServlet extends ...
- Spring boot中使用servlet filter
Spring boot中使用servlet filter liuyuhang原创,未经允许请勿转载! 在web项目中经常需要一些场景,如参数过滤防止sql注入,防止页面攻击,空参数矫正等, 也可以做成 ...
随机推荐
- 20175212童皓桢 实验三敏捷开发与XP实践实验报告
20175212童皓桢 实验三敏捷开发与XP实践实验报告 实验内容 XP基础 XP核心实践 相关工具 实验步骤 一.Code菜单功能的研究 Move Line/statement Down/Up:将某 ...
- 第十一周Java学习总结。
java UI 图形界面知识梳理: ATM: 在整个AWT包中提供的所有工具类主要分为以下3种. (1)组件:Component. (2)容器:Container. (3)布局管理器:LayoutMa ...
- 职位-IT:软件设计师
ylbtech-职位-IT:软件设计师 软件设计师是指能根据软件开发项目管理和软件工程的要求,按照系统总体设计规格说明书进行软件设计,编写程序设计规格说明书等相应的文档的实用性人才.还能够组织和指导程 ...
- apache通过rewrite限制某个目录
1.<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_URI} ^.*/tmp/* [NC] RewriteRule ...
- 阶段3 2.Spring_08.面向切面编程 AOP_7 通用化切入点表达式
下面配置了通用的表达式,,上面的四个就不用再配置那么长 索罗的切入点表达式了. 节省了每次都要写一长段表达式的过程 写在一个aop:aspect这个切面下面就只能当前切面用 写在切面里面 运行测试 挪 ...
- PHP结合Ueditor并修改图片上传路径 微信小程序 拼接域名显示图片
前言 在使用UEditor编辑器时,一般我们都是需要修改默认的图片上传路径的,下面是我整理好的修改位置和方法供大家参考. 操作 Ueditor PHP版本本身自带了一套上传程序,我们可以在此基础中,找 ...
- 修改主机名、hosts解析记录
.hostname和hosts的区别 /etc/hostname中存放的是主机名 /etc/hosts存放的是域名与ip的对应关系 .修改主机名 需要下面两个步骤的结合才可以 2.1.修改网络主机名 ...
- ABAP的smartform赋值
添加文本后, 在输出选项中指定行/列
- centos7 忘记root密码,如何进入单用户模式。
init方法 1.centos7的grub2界面会有两个入口,正常系统入口和救援模式: 2.修改grub2引导 在正常系统入口上按下"e",会进入edit模式,搜寻ro那一行,以l ...
- python—004
一.集合(set) 1.定义:不同的元素组成,无序排列的,可哈希的值(存放不可变类型:数字.字符串.元组) s={1,2,'ww',3,4,5,6,7,8,'ee'}print (type(s))pr ...