springboot自定義Servlet容器

Spring_Boot專欄
上一篇 主目錄 下一篇

【前言】
https://github.com/ShaneHolmes/springboot-project1-ems

什麼是web容器?什麼是servlet容器
web容器(web服務器)主要有:Apache、IIS、Tomcat、Jetty、JBoss、webLogic等,而Tomcat、Jetty、JBoss、webLogic同時也是servlet容器,或者說他們還包含了servlet容器。沒有servlet容器,你也可以用web容器直接訪問靜態頁面,比如安裝一個apache等,但是如果要顯示jsp/servlet,你就要安裝一個servlet容器了,但是光有servlet容器是不夠的,因爲它要被解析成html輸出,所以你仍需要一個web容器。大多數servlet容器同時提供了web容器的功能,也就是說大多servelt容器可以獨立運行你的web應用。更多


1 嵌入式Servlet容器

SpringBoot默認使用Tomcat作爲嵌入式的Servlet容器;
在這裏插入圖片描述

1.1 修改默認Servlet容器的相關配置

1.1.1 在配置文件application.properties中修改

application.properties中修改和server有關的配置

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

server.tomcat.uri-encoding=UTF-8

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

1.1.2 通過Servlet容器的定製器來修改

編寫一個EmbeddedServletContainerCustomizer:嵌入式的Servlet容器的定製器;來修改Servlet容器的配置
springboot2.X版本用以下配置:
config/MyMvcConfig.class

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                factory.setPort(8888);
            }
        };
    }
}

在這裏插入圖片描述

1.2 註冊Servlet三大組件Servlet、Filter、Listener

由於SpringBoot默認是以jar包的方式啓動嵌入式的Servlet容器來啓動SpringBoot的web應用,沒有web.xml文件。

註冊三大組件用以下方式

ServletRegistrationBean

//註冊三大組件
@Bean
public ServletRegistrationBean myServlet(){
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
    return registrationBean;
}

FilterRegistrationBean

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

ServletListenerRegistrationBean

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

SpringBoot幫我們自動SpringMVC的時候,自動的註冊SpringMVC的前端控制器;DIspatcherServlet;

DispatcherServletAutoConfiguration中:

@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public ServletRegistrationBean dispatcherServletRegistration(
      DispatcherServlet dispatcherServlet) {
   ServletRegistrationBean registration = new ServletRegistrationBean(
         dispatcherServlet, this.serverProperties.getServletMapping());
    //默認攔截: /  所有請求;包靜態資源,但是不攔截jsp請求;   /*會攔截jsp
    //可以通過server.servletPath來修改SpringMVC前端控制器默認攔截的請求路徑
    
   registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
   registration.setLoadOnStartup(
         this.webMvcProperties.getServlet().getLoadOnStartup());
   if (this.multipartConfig != null) {
      registration.setMultipartConfig(this.multipartConfig);
   }
   return registration;
}

1.3 替換爲其他嵌入式Servlet容器

默認支持:Tomcat(默認使用)

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

Jetty(高性能非阻塞的servlet容器,適用於長連接)

<!-- 引入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>

Undertow(不支持jsp)

<!-- 引入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>

2 外置的Servlet容器

嵌入式Servlet容器:應用打成可執行的jar

  • 優點:簡單、便攜
  • 缺點:默認不支持JSP、優化定製比較複雜(使用定製器【ServerProperties、自定義EmbeddedServletContainerCustomizer】,自己編寫嵌入式Servlet容器的創建工廠【WebServerFactoryCustomizer】)

外置的Servlet容器:外面安裝Tomcat—應用war包的方式打包

2.1 步驟

1)必須創建一個war項目(利用idea創建好目錄結構)
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
2)創建web.xml文件:
在這裏插入圖片描述
3)配置運行的服務器:
在這裏插入圖片描述
4)部署項目
在這裏插入圖片描述

5)將嵌入式的Tomcat指定爲provided

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <scope>provided</scope>
</dependency>

6)必須編寫一個SpringBootServletInitializer的子類,並調用configure方法

public class ServletInitializer extends SpringBootServletInitializer {

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

}

4)啓動服務器就可以使用

啓動Servlet容器,再啓動SpringBoot應用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章