一.查看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>

最新文章

  1. SAP&#160;MM常用表
  2. Masonry 固定宽度 等间距
  3. NServiceBus教程-NServiceBus和WCF
  4. CentOS 6.6下Redis安装
  5. Storm系列(九)架构分析之Supervisor-同步Nimbus的事件线程
  6. CentOS 6.7下iPython提示“WARNING: Readline services not available or not loaded.”的解决办法
  7. Android修改XML文件
  8. leetcode:Multiply Strings(字符串的乘法)【面试算法题】
  9. out和ref之间的区别
  10. Obj-C 实现 QFileDialog函数
  11. Android startActivity原理分析(基于Android 8.1 AOSP)
  12. django系列 2 :启动应用,目录结构解读
  13. 页面对象(Page Object)模式
  14. ES6 扩展运算符 三点(...)
  15. jQuery 选择器 筛选器 样式操作 文本操作 属性操作 文档处理 事件 动画效果 插件 each、data、Ajax
  16. nodejs通过request请求远程url的文件并下载到本地
  17. Android: TextView 及其子类通过代码和 XML 设置字体大小的存在差异的分析
  18. Unity打安卓包 Android 所有错误解决方案大全(几乎囊括所有打包错误 )
  19. tensorflow初次接触记录,我用python写的tensorflow第一个模型
  20. git push失败

热门文章

  1. for循环遍历对比
  2. Python学习笔记:字典型的数据结构
  3. yconsole使用说明
  4. 如何在form组件中添加一个单选或者多选的字段
  5. Jmeter(六)事务
  6. Prism学习--实现可插拔的模块
  7. &lt;table&gt;表格与jqGrid
  8. SweetAlert(弹出层插件)
  9. Vue+Python 电商实战
  10. django 如何传递id 参数