一.查看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容器的更多相关文章

  1. (7)Spring Boot web开发 --- servlet容器

    文章目录 配置嵌入式 Servlet 容器 注册 三大组件 使用其他 servlet 容器 使用外置的 `Servlet` 容器 配置嵌入式 Servlet 容器 Spirng Boot 默认使用自带 ...

  2. spring boot项目发布tomcat容器(包含发布到tomcat6的方法)

    spring boot因为内嵌tomcat容器,所以可以通过打包为jar包的方法将项目发布,但是如何将spring boot项目打包成可发布到tomcat中的war包项目呢? 1. 既然需要打包成wa ...

  3. 【串线篇】spring boot嵌入式Servlet容器启动原理;

    什么时候创建嵌入式的Servlet容器工厂?什么时候获取嵌入式的Servlet容器并启动Tomcat: 获取嵌入式的Servlet容器工厂: 1).SpringBoot应用启动运行run方法 2).r ...

  4. 【串线篇】spring boot嵌入式Servlet容器自动配置原理

    EmbeddedServletContainerAutoConfiguration:嵌入式的Servlet容器自动配置? @AutoConfigureOrder(Ordered.HIGHEST_PREC ...

  5. Spring Boot 嵌入式Web容器

    目录 前言 1.起源 2.容器启动流程解析 2.1.获取应用类型 2.2.容器启动流程 3.加载 Web 容器工厂 4.总结 前言         最近在学习Spring Boot相关的课程,过程中以 ...

  6. Spring Boot之注册servlet三大组件

    由于Spring Boot默认是以jar包的形式启动嵌入式的Servlet容器来启动Spring Boot的web应用是,没有web.xml配置文件 注册三大组件用以下方式 ServletRegist ...

  7. spring boot 加载web容器tomcat流程源码分析

    spring boot 加载web容器tomcat流程源码分析 我本地的springboot版本是2.5.1,后面的分析都是基于这个版本 <parent> <groupId>o ...

  8. 18. Spring Boot 、注册Servlet三大组件Servlet、Filter、Listener

    由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件 public class MyServlet extends ...

  9. Spring boot中使用servlet filter

    Spring boot中使用servlet filter liuyuhang原创,未经允许请勿转载! 在web项目中经常需要一些场景,如参数过滤防止sql注入,防止页面攻击,空参数矫正等, 也可以做成 ...

随机推荐

  1. XPath2Doc,一个半自动采集网页生成Word Docx文件的工具,带企查查和天眼查模板

    原始出处:https://www.cnblogs.com/Charltsing/p/XPath2Doc.html 很多人需要从网站采集一些数据填写Word模板,手工操作费时费力还容易出错,所以我给朋友 ...

  2. Firefox63以后 禁止自动更新方式

    参考:https://bbs.kafan.cn/thread-2135160-1-1.html 63版以后在prefs.js文件末尾加代码来禁止自动更新的方式失效 新方式: 使用DisableAppU ...

  3. OpenCV学习笔记(8)——图像平滑

    使用不同的低筒滤波器对图像进行模糊 使用自定义的率弄起对图像进行卷积(2D卷积) 2D卷积 与信号一样,我们也可以对2D图像实施低通滤波,高通滤波等.LPF帮助我们去除噪声,模糊图像.而HPF帮助我们 ...

  4. Rate 评分

    评分组件 基础用法 评分被分为三个等级,可以利用颜色对分数及情感倾向进行分级(默认情况下不区分颜色).三个等级所对应的颜色用过colors属性设置,而它们对应的两个阈值则通过 low-threshol ...

  5. linux如何杀掉进程(kill)

    方法/步骤1: 使用“ps -e|grep mysql”命令,查看mysql程序的对应的pid号.结果如下图:   方法/步骤2: 使用“kill -9 2891”命令,可以结束掉mysqld_saf ...

  6. Python_编程特色

    目录 目录 前言 软件环境 列表推导式 字典的默认值 forelse语句 交换两个变量的值 链式比较 真值测试 序列类型元素反转 连接字符串和列表 内置算术函数 利用zip来创建键值对 最后 前言 P ...

  7. maven将自己的springboot项目打包成jar包后,作为工具包引入其他项目,找不到jar中的类

    将springboot项目打包成jar包,作为工具包导入项目后,找不到jar中的类. 原因是:springboot项目使用了自动的打包插件. 原先的插件配置: <build> <pl ...

  8. 布局复习---BFC

    其实在一开始我是没有BFC的这个概念的,只是知道在浮动过后,后续的元素如果出现问题,就做我们常说的:overflow:hidden.其中的原因还是不甚了解.不是说以前老师没有讲解过,而是以前根本就没有 ...

  9. 如何获取当前包名与activitity&&抓log

    若hi1:获取当前包名以及Activity (1)adb shell dumpsys activity | find "mFocusedActivity" (2)adb shell ...

  10. 使用FreeHttp任意篡改http报文 (FreeHttp使用及实现说明)

    本文转自:https://www.cnblogs.com/lulianqi/p/10428551.html 前言 FreeHttp是一个Fiddler插件借助FreeHttp您可按照您自己的设定修改请 ...