摘要:生長速度越快的樹木,其致密度就越低,生命往往也越短暫而松樹柏樹胡楊等樹種,要上百年才能成材,用起來卻可千年不朽。
Spring Boot之 Servlet、Fiter、Listener的使用
新建項目:http://start.spring.io/
Servlet的使用新建MyServlet類
package com.adagio.chat.servlet; import java.io.IOException; import java.io.PrintWriter; import java.io.Writer; 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( name = "myservlet", urlPatterns = {"/myservlet1", "/myservlet2"} ) public class MyServlet extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ Writer writer = response.getWriter(); writer.write("Hello World"); } }
啟動類添加注解 @ServletComponentScan(basePackages = {"com.adagio.chat.servlet"})
啟動運行訪問:http://localhost:8080/myservlet1和http://localhost:8080/myservlet2
servlet的使用很簡單:
添加注解 @WebServlet
配置name,可以不用配置
配置路徑,urlPatterns 可以配置多值
Filter的使用使用方式和Servlet十分類似
package com.adagio.chat.servlet; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.filter.OncePerRequestFilter; //@WebFilter(servletNames = "myservlet") @WebFilter(urlPatterns = "/myservlet1") public class MyFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { ServletContext servletContext = getServletContext(); servletContext.log("url myservlet was filter"); filterChain.doFilter(request, response); } }
可以直接使用servletNames過濾,如@WebFilter(servletNames = "myservlet"),訪問http://localhost:8080/myservlet1和http://localhost:8080/myservlet2都會在控制臺打印日志
也可以使用@WebFilter(urlPatterns = "/myservlet1"),訪問上面兩個鏈接,只有訪問http://localhost:8080/myservlet1這個地址才會打印日志
Listener的使用使用方式也是類似的
package com.adagio.chat.servlet; import javax.servlet.ServletContext; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpServletRequest; @WebListener public class MyListener implements ServletRequestListener { @Override public void requestDestroyed(ServletRequestEvent sre) { HttpServletRequest request = (HttpServletRequest)sre.getServletRequest(); ServletContext servletContext = request.getServletContext(); servletContext.log("request was destroyed"); } @Override public void requestInitialized(ServletRequestEvent sre) { HttpServletRequest request = (HttpServletRequest)sre.getServletRequest(); ServletContext servletContext = request.getServletContext(); servletContext.log("request was initialized"); } }
訪問http://localhost:8080/myservlet2 觀察控制臺
執行順序2017-09-10 16:11:18.247 INFO 7692 --- [io-8080-exec-10] o.a.c.c.C.[Tomcat].[localhost].[/] : request was initialized 2017-09-10 16:11:18.271 INFO 7692 --- [io-8080-exec-10] o.a.c.c.C.[Tomcat].[localhost].[/] : url myservlet was filter 2017-09-10 16:11:18.271 INFO 7692 --- [io-8080-exec-10] o.a.c.c.C.[Tomcat].[localhost].[/] : servlet was doGet 2017-09-10 16:11:18.275 INFO 7692 --- [io-8080-exec-10] o.a.c.c.C.[Tomcat].[localhost].[/] : request was destroyed
先監聽器的初始化,再經過濾器,然后執行doGet方法,最后執行監聽器的消亡
使用注入的方式注冊效果和上面的方式一樣的,具體實現見代碼
package com.adagio.chat.spring.boot; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.filter.OncePerRequestFilter; public class MyFilter2 extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { // ServletContext servletContext = getServletContext(); // // //動態獲取URI // String requestURI = request.getRequestURI(); // // servletContext.log("url "+requestURI+" was filter from MyFilter2"); doSomething(); filterChain.doFilter(request, response); } /** * 當沒有request的時候,可以使用這種方式獲取request */ public void doSomething(){ RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)requestAttributes; HttpServletRequest request = servletRequestAttributes.getRequest(); ServletContext servletContext = getServletContext(); //動態獲取URI String requestURI = request.getRequestURI(); servletContext.log("url "+requestURI+" was filter from MyFilter2"); } }
package com.adagio.chat.spring.boot; import java.io.IOException; import java.io.PrintWriter; import java.io.Writer; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet2 extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ ServletContext servletContext = request.getServletContext(); servletContext.log("servlet2 was doGet"); Writer writer = response.getWriter(); writer.write("Hello World From MyServlet2"); } }
package com.adagio.chat; import javax.servlet.DispatcherType; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import com.adagio.chat.servlet.MyListener; import com.adagio.chat.spring.boot.MyFilter2; import com.adagio.chat.spring.boot.MyServlet2; @SpringBootApplication @ServletComponentScan(basePackages = {"com.adagio.chat.servlet"}) public class ChatApplication { public static void main(String[] args) { SpringApplication.run(ChatApplication.class, args); } @Bean public static ServletRegistrationBean servletRegistrationBean(){ ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(); servletRegistrationBean.setServlet(new MyServlet2()); servletRegistrationBean.addUrlMappings("/spring/boot/myservelt2"); servletRegistrationBean.setName("myservlet2"); return servletRegistrationBean; } @Bean public static FilterRegistrationBean filterRegistrationBean(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new MyFilter2()); // filterRegistrationBean.addUrlPatterns("/spring/boot/myservelt2"); filterRegistrationBean.addServletNames("myservlet2"); filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE); return filterRegistrationBean; } /** * 監聽器會監聽所有請求 * 可以重復注冊監聽器 * @return */ @Bean public static ServletListenerRegistrationBeanservletListenerRegistrationBean(){ ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean<>(new MyListener()); return servletListenerRegistrationBean; } }
急事慢做:成長是長時間努力、積累和進步的結果,是水到渠成的事情,絕不是心急就能做到的。
生長速度越快的樹木,其致密度就越低,生命往往也越短暫;而松樹、柏樹、胡楊等樹種,要上百年才能成材,用起來卻可千年不朽。 - 俞敏洪
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/70385.html
摘要:圖片抓取上本項目主要講述項目的搭建和啟動過程,為以后繼續圖片抓取的業務展示做基礎。用于處理請求和響應的攔截處理。這樣相比容器直接發到處理,大大減少了代碼重復工作而且方便統一管理。上下文關系從上圖可以看出主要在和兩部分做工作。 【圖片抓取】003-JAVA WEB(上) 本項目主要講述java web項目的搭建和啟動過程,為以后繼續圖片抓取的業務展示做基礎。項目中采用tomcat+spr...
摘要:實體代碼如下所示恒宇少年碼云用戶名密碼創建內添加一個注冊方法,該方法只是實現注冊事件發布功能,代碼如下所示恒宇少年碼云用戶注冊方法省略其他邏輯發布事件事件發布是由對象管控的,我們發布事件前需要注入對象調用方法完成事件發布。 ApplicationEvent以及Listener是Spring為我們提供的一個事件監聽、訂閱的實現,內部實現原理是觀察者設計模式,設計初衷也是為了系統業務邏輯之...
摘要:基于,支持后面的版本二基本使用網頁生成訪問生成項目并下載。這里的配置項可以自動提示。數據配置文件配置四整合映射跟之前一樣注冊注冊攔截器五整合基本整合使用映射文件映射文件和注解同時存在注解引入配置文件增加事務六自動配置自動原理配置項 一、SpringBoot簡介 1. 使用SSM開發項目的時候有什么不足之處 創建ssm項目比較麻煩 配置比較麻煩 依賴的配置比較多比較麻煩 手動集成第三方...
摘要:慕課網消息中間件極速入門與實戰學習總結時間年月日星期三說明本文部分內容均來自慕課網。 慕課網《RabbitMQ消息中間件極速入門與實戰》學習總結 時間:2018年09月05日星期三 說明:本文部分內容均來自慕課網。@慕課網:https://www.imooc.com 教學源碼:無 學習源碼:https://github.com/zccodere/s... 第一章:RabbitM...
閱讀 2516·2021-11-18 10:02
閱讀 1985·2021-11-09 09:45
閱讀 2431·2021-09-26 09:47
閱讀 1028·2021-07-23 10:26
閱讀 1070·2019-08-30 15:47
閱讀 3362·2019-08-30 15:44
閱讀 973·2019-08-30 15:43
閱讀 888·2019-08-29 13:50