一.查看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. JSTL标签语言

    ******概述: JSTL是apache对EL表达式的扩展(也就是说JSTL依赖EL),JSTL是标签语言! JSTL标签使用以来非常方便,它与JSP动作标签一定,只不过它不是JSP内置的标签,需要 ...

  2. 190707Python-RabbitMQ

    一.简单的RabbitMQ示例 生产者 # Author:Li Dongfei import pika connection = pika.BlockingConnection( pika.Conne ...

  3. 三、Linux下mysql的完整安装

    参考资料:http://www.cnblogs.com/fly1988happy/archive/2011/11/21/2257682.html http://www.cnblogs.com/xion ...

  4. [论文理解] CapsuleNet

    CapsuleNet 前言 找了很多资料,终于把整个流程搞懂了,其实要懂这个运算并不难,难的对我来说是怎么用代码实现,也找了github上的一些代码来看,对我来说都有点冗长,变量分布太远导致我脑袋炸了 ...

  5. P2010 回文日期

    P2010 回文日期 题解 回文日期,一共8位,只需要枚举4位就好了其实,然后判断它是否合法 代码 #include<bits/stdc++.h> using namespace std; ...

  6. java 传值

    好文章:https://zwmf.iteye.com/blog/1738574 public class Test { public int i,j; public void test_m(Test ...

  7. 配置pip镜像源(转)

    使用pip安装python扩展时,如若没有配置国内镜像源,在未翻墙的情况下,其下载速度将会特别缓慢.因此,有些时候我们必须使用国内镜像源,来解决pip下载安装速度慢的问题,比较常用的国内镜像包括: 阿 ...

  8. elasticsearch head + xpack 用户名密码访问

    修改配置文件elasticsearch.yml,增加http.cors.allow-headers: Authorization 访问head时,url如下所示:http://192.168.100. ...

  9. Unity3D中的SendMessage使用(消息传递的三种方法)

    概述 Unity提供的消息推送机制可以非常方便我们的脚本开发,它实现的是一种伪监听者模式,利用的是反射机制. 常用函数 关于消息推送,常用的函数有三个:”SendMessage“.”SendMessa ...

  10. 【Qt开发】Qt5.7串口开发

    QT5有专门的串口类:  QSerialPort:提供访问串口的功能  QSerialPortInfo:提供系统中存在的串口的信息  具体使用方法:  1.在pro文件中加入: QT += seria ...