zoukankan      html  css  js  c++  java
  • 【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=8081
    server.context‐path=/demo
    server.tomcat.uri‐encoding=UTF‐8
    #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>
  • 相关阅读:
    云计算下一浪潮,腾讯云抢占 Serverless 制高点
    Serverless + GitHub Actions 完美自动化部署静态网站
    Serverless 技术在格灵深瞳的落地实践
    万物皆可 Serverless 之我的 Serverless 之路
    LeetCode 哈希表 136:只出现一次的数字(计数哈希表,异或)
    Spring 01 IOC
    LeetCode 数组:1.两数之和 11. 盛最多水的容器
    LeetCode 链表:21合并两个有序链表 19删除链表的倒数第N个节点
    Maybatis的一些总结(三:增删改查)
    Maybatis的一些总结(一:了解)
  • 原文地址:https://www.cnblogs.com/ywb-articles/p/10643977.html
Copyright © 2011-2022 走看看