SpringBoot-Servlet容器、组件学习

SpringBoot-Servlet容器、组件学习

  • 一、配置嵌入式的Servlet容器
  • 二、注册Servlet、Filter、Listener
  • 三、其他的嵌入式Servlet容器

一、配置嵌入式的Servlet容器

背景:
SpringBoot默认使用Tomcat作为嵌入式的Servlet容器
思考:
1)SpringBoot中如何定制和修改Servlet容器的相关配置?
1、在配置文件中配置server

server.port=8080
server.servlet.context-path=/crud
server.tomcat.uri-encoding=GBK
//通用的Servlet容器设置
server.xxx
//Tomcat设置
server.tomcat.xxx

2、编写一个嵌入式的Servlet容器的定制器(WebServerFactoryCustomizer);来修改配置
SpringBoot-Servlet容器、组件学习_第1张图片

@Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer()
    {
     
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
     
            //定制嵌入式的Servlet容器相关规则
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
     
                factory.setPort(8083);
            }
        };
    }

题外话:
SpringBoot-Servlet容器、组件学习_第2张图片

二、注册Servlet、Filter、Listener

1)注册Servlet(ServletRegistrationBean)
配置类:
SpringBoot-Servlet容器、组件学习_第3张图片

    @Bean
    public ServletRegistrationBean myServlet()
    {
     
        ServletRegistrationBean servletRegistrationBean=  new ServletRegistrationBean(new MyServlet(),"/myServlet");
        return servletRegistrationBean;
    }

MyServlet类:
SpringBoot-Servlet容器、组件学习_第4张图片

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,MyServlet");
    }
}

2)注册Filter
alt+Enter是返回变量快捷键
SpringBoot-Servlet容器、组件学习_第5张图片

 @Bean
    public FilterRegistrationBean myFilter()
    {
     
        FilterRegistrationBean filterRegistrationBean= new  FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return filterRegistrationBean;
    }

3)注册Listener
SpringBoot-Servlet容器、组件学习_第6张图片

@Bean
    public ServletListenerRegistrationBean myListener()
    {
     
        ServletListenerRegistrationBean<MyListener> myListenerServletListenerRegistrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return myListenerServletListenerRegistrationBean;
    }

三、其他的嵌入式Servlet容器

Tomcat
Jetty(长连接)
Undertow(不支持JSP)
SpringBoot默认可切换以上2个容器
SpringBoot-Servlet容器、组件学习_第7张图片
1、切换jetty,删除tomcat相关依赖,加入jetty依赖

<!--引入其他的Servlet容器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

2、Undertow

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

3、引入web模块默认使用tomcat

你可能感兴趣的:(SpringBoot)