一.查看SpringBoot默认的嵌入式Servlet容器(默认使用的是tomcat)

  在IDEA的项目的pom文件中按Ctrl + shift + Alt + U可以打开SpringBoot依赖的图表,如下所示

    Spring Boot嵌入式的Servlet容器-LMLPHP

    可以发现默认嵌入式的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.测试:

    启动项目后发现控制台输出

      Spring Boot嵌入式的Servlet容器-LMLPHP

    访问 /myServlet 后发现页面输出

      Spring Boot嵌入式的Servlet容器-LMLPHP

    访问/myFilter后,控制台输出

      Spring Boot嵌入式的Servlet容器-LMLPHP

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

      Spring Boot嵌入式的Servlet容器-LMLPHP

四.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>
05-11 22:18