1. 程式人生 > >[SpringBoot之一入門] 08-配置Servlet容器

[SpringBoot之一入門] 08-配置Servlet容器

概要

1.嵌入式Servlet容器
2.註冊Servlet三大元件(Servlet、Filter、Listener)
3.替換為其他嵌入式Servlet容器
4.使用外接的Servlet容器

一、嵌入式Servlet容器引數修改

如何定製和修改Servlet容器的相關配置

1、修改和server有關的配置(ServerProperties【其實也是EmbeddedServletContainerCustomizer】);

//通用的Servlet容器設定
server.xxx
server.port=8088
server.context-path=/order

//Tomcat的設定
server
.tomcat.xxx server.tomcat.uri-encoding=UTF-8

2、編寫一個EmbeddedServletContainerCustomizer:嵌入式的Servlet容器的定製器;來修改Servlet容器的配置

@Configuration
public class MyServerConfig {

    //配置嵌入式的Servlet容器
    @Bean
    public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
        return new
EmbeddedServletContainerCustomizer() { //定製嵌入式的Servlet容器相關的規則 @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(8083); } }; } }

二、註冊Servlet三大元件(Servlet、Filter、Listener)

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

1. ServletRegistrationBean元件

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

2. FilterRegistrationBean元件

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() {
    }
}

3. ServletListenerRegistrationBean元件

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 {

    //1. ServletRegistrationBean元件
    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }

    //2. 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;
}

三、替換為其他嵌入式Servlet容器

Tomcat(預設使用)

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   引入web模組預設就是使用嵌入式的Tomcat作為Servlet容器;
</dependency>

Jetty

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

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

四、使用外接的Servlet容器

嵌入式Servlet容器:應用打成可執行的jar
    優點:簡單、便攜;
    缺點:預設不支援JSP、優化定製比較複雜
       使用定製器【ServerProperties、自定義EmbeddedServletContainerCustomizer】,
       或自己編寫嵌入式Servlet容器的建立工廠【EmbeddedServletContainerFactory】;
外接的Servlet容器:應用打包成war包;

步驟

1. 必須建立一個war專案

這裡寫圖片描述

專案目錄結構 這裡寫圖片描述

2. 將嵌入式的Tomcat指定為provided;

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

3. 必須編寫一個SpringBootServletInitializer的子類,並呼叫configure方法

public class ServletInitializer extends SpringBootServletInitializer {

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

}

4. 啟動伺服器

這裡寫圖片描述 這裡寫圖片描述

jsp頁面訪問

spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp