SpringBoot2.x系列教程39--整合Servlets, Filters和listeners

一. Spring Boot中Servlets,Filters和Listeners
1. 概述
在Spring Boot中使用內嵌servlet容器時,我們可以通過使用Spring beans或掃描Servlet組件的方式註冊Servlets,Filters及特定Servlet相關的所有listeners(比如HttpSessionListener)。
Spring Boot 提供了 ServletRegistrationBean, FilterRegistrationBean, ServletListenerRegistrationBean 三個類分別用來註冊 Servlet, Filter, Listener。
示例代碼:
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author 一一哥 */ public class RegisterServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { String name = getServletConfig().getInitParameter("name"); String sex = getServletConfig().getInitParameter("sex"); resp.getOutputStream().println("name is " + name); resp.getOutputStream().println("sex is " + sex); } } @Bean public ServletRegistrationBean registerServlet() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean( new RegisterServlet(), "/registerServlet"); servletRegistrationBean.addInitParameter("name", "一一哥"); servletRegistrationBean.addInitParameter("sex", "man"); return servletRegistrationBean; }
2. Servlet 3.0組件掃描註冊
Servlet 3.0 之前,Servlet、Filter、Listener 這些組件都需要在 web.xml 中進行配置,3.0 之後開始不再需要 web.xml 這個配置文件了,所有的組件都可以通過代碼配置或者註解來達到目的。
Servlet 3.0後提供了3個註解來代替:

  • @WebServlet代替 servlet 配置;
  • @WebFilter代替 filter 配置;
  • @WebListener代替 listener 配置

二. Spring Boot中Servlet的使用
接下來我帶大家講解一下Spring Boot中實現Servlet的開發,主要是講解兩種實現方式,組件註冊方式和註解實現方式。
我們首先創建一個Spring boot的web項目,添加web的相關依賴包,具體過程略。

1. 組件註冊方式
1.1 創建UserServlet類
package com.yyg.boot.web; import lombok.extern.slf4j.Slf4j; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @Author 一一哥Sun * @Date Created in 2020/3/29 * @Description Description */ @Slf4j public class UserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { log.warn("第一種Servlet實現方式-->組件註冊方式"); resp.getWriter().write("success"); } }
1.2 註冊Servlet組件
我們可以在啓動類Application或者別的配置類中,利用ServletRegistrationBean來註冊剛纔創建的Servlet組件。
package com.yyg.boot; import com.yyg.boot.web.UserServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; /** * @Author 一一哥Sun * @Date Created in 2020/3/29 * @Description Description */ @SpringBootApplication public class ServletApplication { public static void main(String[] args){ SpringApplication.run(ServletApplication.class,args); } /** *ServletRegistrationBean:註冊創建的Servlet類 */ @Bean public ServletRegistrationBean getServletRegistrationBean(){ ServletRegistrationBean bean = new ServletRegistrationBean(new UserServlet()); //url映射路徑 bean.addUrlMappings("/user"); return bean; } }
1.3 啓動程序進行測試
我們啓動程序,在瀏覽器輸入
http://localhost:8080/user
可以看到控制檯中成功輸出如下信息。

瀏覽器中顯示如下內容:

2. 註解實現方式
2.1 創建OrderServlet類
創建OrderServlet類,在該類上添加@WebServlet註解。
package com.yyg.boot.web; import lombok.extern.slf4j.Slf4j; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @Author 一一哥Sun * @Date Created in 2020/3/29 * @Description 第二種實現Servlet的方式, 通過@WebServlet註解來實現註冊. */ @Slf4j @WebServlet(name = "OrderServlet", urlPatterns = "/order") public class OrderServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { log.warn("第2種Servlet實現方式-->註解實現方式"); resp.getWriter().write("success"); } }
2.2 添加@ServletComponentScan註解
注意,如果使用的是 Spring Boot 內嵌服務器,需要在配置類上面添加額外的 @ServletComponentScan 註解來開啓 Servlet 組件掃描功能,如果使用的是獨立的服務器,則不需要添加,會使用服務器內部的自動發現機制。
package com.yyg.boot; import com.yyg.boot.web.UserServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; /** * @Author 一一哥Sun * @Date Created in 2020/3/29 * @Description Description */ @SpringBootApplication @ServletComponentScan(basePackages = "com.yyg.boot.web") public class ServletApplication { public static void main(String[] args){ SpringApplication.run(ServletApplication.class,args); } }
2.3 啓動程序進行測試
我們啓動程序,在瀏覽器輸入
http://localhost:8080/user
可以看到控制檯中成功輸出如下信息:


瀏覽器中顯示如下內容:

三. Filter與Listener的使用
Spring Boot中Filter、Listener的使用,與Servlet的用法類似,在此不進行一一講解,感興趣的朋友可以自行嘗試。

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