Web開發使用 Controller 基本上能夠完成大部分需求,可是咱們還可能會用到Servlet、Filter、Listener、Interceptor 等等。html
當使用Spring-Boot時,嵌入式Servlet容器經過掃描註解的方式註冊Servlet、Filter和Servlet規範的全部監聽器(如HttpSessionListener監聽器)。
Spring boot 的主 Servlet 爲 DispatcherServlet,其默認的url-pattern爲「/」。也許咱們在應用中還須要定義更多的Servlet。java
在spring boot中添加本身的Servlet有兩種方法,代碼註冊Servlet和註解自動註冊(Filter和Listener也是如此)。
1、代碼註冊經過ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 得到控制。
也能夠經過實現 ServletContextInitializer 接口直接註冊。spring
2、在 SpringBootApplication 上使用@ServletComponentScan註解後,Servlet、Filter、Listener 能夠直接經過 @WebServlet、@WebFilter、@WebListener 註解自動註冊,無需其餘代碼。session
註解Servletide
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(urlPatterns="/myServlet2/*", description="Servlet的說明") public class MyServlet2 extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<"); doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<"); resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Hello World</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>這是:myServlet2</h1>"); out.println("</body>"); out.println("</html>"); } }
註解式過濾器 url
import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; /** * * 使用註解標註過濾器 * @WebFilter將一個實現了javax.servlet.Filter接口的類定義爲過濾器 * 屬性filterName聲明過濾器的名稱,可選 * 屬性urlPatterns指定要過濾的URL模式,也可以使用屬性value來聲明.(指定要過濾的URL模式是必選屬性) */ @WebFilter(filterName="myFilter",urlPatterns="/*") publicclass MyFilter implements Filter{ @Override publicvoid init(FilterConfig config) throws ServletException { System.out.println("過濾器初始化"); } @Override publicvoid doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("執行過濾操做"); chain.doFilter(request, response); } @Override publicvoid destroy() { System.out.println("過濾器銷燬"); } }
註解式監聽器spa
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; /** * 使用@WebListener註解,實現ServletContextListener接口 * * @author Angel(QQ:412887952) * @version v.0.1 */ @WebListener public class MyServletContextListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent arg0) { System.out.println("ServletContex銷燬"); } @Override public void contextInitialized(ServletContextEvent arg0) { System.out.println("ServletContex初始化"); } }
監聽Session的建立與銷燬code
import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * 監聽Session的建立與銷燬 * */ @WebListener public class MyHttpSessionListener implements HttpSessionListener { @Override publicvoid sessionCreated(HttpSessionEvent se) { System.out.println("Session 被建立"); } @Override publicvoid sessionDestroyed(HttpSessionEvent se) { System.out.println("ServletContex初始化"); } }
注意不要忘記在 SpringBootSampleApplication.java 上添加 @ServletComponentScan 註解。htm
啓動的過程當中咱們會看到輸出:接口
ServletContex初始化
過濾器初始化
服務啓動後,隨便訪問一個頁面,會看到輸出:
執行過濾操做
Session 被建立