SpringBoot如何配置嵌入式Servlet容器

SpringBoot默认使用Tomcat作为嵌入式的Servlet容器
在这里插入图片描述

1. 如何定制和修改Servlet容器的相关配置

1.1 修改和server有关的配置

1.1.1 通过配置文件来修改

server.port=8081
server.context-path=/crud

server.tomcat.uri-encoding=UTF-8

//通用的Servlet容器设置
server.xxx
//Tomcat的设置
server.tomcat.xxx

1.1.2 编写EmbeddedServletContainerCustomizer


@Bean  //一定要将这个定制器加入到容器中
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
    
    
    return new EmbeddedServletContainerCustomizer() {
    
    
        //定制嵌入式的Servlet容器相关的规则
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
    
    
            container.setPort(8083);
        }
    };
}

2. 注册Servlet三大组件

2.1 注册Servlet

首先编写一个Servlet

public class MyServlet extends HttpServlet {
    
    

    //处理get请求
    @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");
    }
}

然后将编写的Servlet注入容器

@Configuration
public class MyServerConfig {
    
    

    //注册三大组件
    @Bean
    public ServletRegistrationBean myServlet(){
    
    
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }
}

2.2 注册Filter

首先编写一个过滤器

public class MyFilter implements Filter {
    
    

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    
    

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    
    
        System.out.println("MyFilter process...");
        chain.doFilter(request,response);

    }

    @Override
    public void destroy() {
    
    
    }
}

然后将编写的过滤器注入

@Configuration
public class MyServerConfig {
    
    
    @Bean
    public FilterRegistrationBean myFilter(){
    
    
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return registrationBean;
    }

2.3 注册Listener

首先编写一个监听器

public class MyListener implements ServletContextListener {
    
    
    @Override
    public void contextInitialized(ServletContextEvent sce) {
    
    
        System.out.println("contextInitialized...web应用启动");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    
    
        System.out.println("contextDestroyed...当前web项目销毁");
    }
}

再注入监听器

@Configuration
public class MyServerConfig {
    
    
    @Bean
    public ServletListenerRegistrationBean myListener(){
    
    
        ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return registrationBean;
    }
}

3.将Tomca替换为其他嵌入式Servlet容器

我们在ConfigurableEmbeddedServletContainer接口的实现类工厂中,发现SpringBoot中默认的内置Servlet容器有三种:
在这里插入图片描述
Tomcat是默认使用的

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   引入web模块默认就是使用嵌入式的Tomcat作为Servlet容器;
</dependency>

下面我们来修改默认的设置:

<!-- 引入web模块 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <exclusions>
      <exclusion>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <groupId>org.springframework.boot</groupId>
      </exclusion>
   </exclusions>
</dependency>

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

我们先排除Tomcat,然后再引入其他的容器。
同理

<!-- 引入web模块 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <exclusions>
      <exclusion>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <groupId>org.springframework.boot</groupId>
      </exclusion>
   </exclusions>
</dependency>

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

4. 使用外置的Servlet容器

嵌入式的Servlet容器:应用打成可执行的jar
优点:简单便携
缺点:默认不支持JSP、优化定制比较复杂
外置的Servlet容器:外面安装Tomcat,应用以war包的方式打包
步骤如下:

  • 创建一个war项目;(可以利用idea创建好目录结构)
  • 将Tomcat指定为provided,idea创建目录结构时自动更改。
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <scope>provided</scope>
</dependency>
  • 编写一个SpringBootServletInitializer的子类,并调用configure方法,如果时IDEA创建的目录结构,可以发现此类已经创建
public class ServletInitializer extends SpringBootServletInitializer {
    
    

   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    
    
       //传入SpringBoot应用的主程序
      return application.sources(SpringBoot04WebJspApplication.class);
   }
}

除此之外,还要注意的一点就是项目结构的问题。
在这里插入图片描述
在这里插入图片描述
利用IDEA的便捷方式创建好相应目录后就可以编写业务代码,并启动应用。

猜你喜欢

转载自blog.csdn.net/weixin_44726976/article/details/109328198