六、Web服务器——FilterListener 学习笔记
生活随笔
收集整理的這篇文章主要介紹了
六、Web服务器——FilterListener 学习笔记
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今日內容
1. Filter:過濾器 2. Listener:監聽器Filter:過濾器
1. 概念:* 生活中的過濾器:凈水器,空氣凈化器,土匪、* web中的過濾器:當訪問服務器的資源時,過濾器可以將請求攔截下來,完成一些特殊的功能。* 過濾器的作用:* 一般用于完成通用的操作。如:登錄驗證、統一編碼處理、敏感字符過濾...2. 快速入門:1. 步驟:1. 定義一個類,實現接口Filter2. 復寫方法3. 配置攔截路徑1. web.xml2. 注解2. 代碼:@WebFilter("/*")//訪問所有資源之前,都會執行該過濾器public class FilterDemo1 implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println("filterDemo1被執行了....");//放行filterChain.doFilter(servletRequest,servletResponse);}@Overridepublic void destroy() {}}3. 過濾器細節:1. web.xml配置 <filter><filter-name>demo1</filter-name><filter-class>cn.itcast.web.filter.FilterDemo1</filter-class></filter><filter-mapping><filter-name>demo1</filter-name><!-- 攔截路徑 --><url-pattern>/*</url-pattern></filter-mapping>2. 過濾器執行流程1. 執行過濾器2. 執行放行后的資源3. 回來執行過濾器放行代碼下邊的代碼3. 過濾器生命周期方法1. init:在服務器啟動后,會創建Filter對象,然后調用init方法。只執行一次。用于加載資源2. doFilter:每一次請求被攔截資源時,會執行。執行多次3. destroy:在服務器關閉后,Filter對象被銷毀。如果服務器是正常關閉,則會執行destroy方法。只執行一次。用于釋放資源4. 過濾器配置詳解* 攔截路徑配置:1. 具體資源路徑: /index.jsp 只有訪問index.jsp資源時,過濾器才會被執行2. 攔截目錄: /user/* 訪問/user下的所有資源時,過濾器都會被執行3. 后綴名攔截: *.jsp 訪問所有后綴名為jsp資源時,過濾器都會被執行4. 攔截所有資源:/* 訪問所有資源時,過濾器都會被執行* 攔截方式配置:資源被訪問的方式* 注解配置:* 設置dispatcherTypes屬性1. REQUEST:默認值。瀏覽器直接請求資源2. FORWARD:轉發訪問資源3. INCLUDE:包含訪問資源4. ERROR:錯誤跳轉資源5. ASYNC:異步訪問資源* web.xml配置* 設置<dispatcher></dispatcher>標簽即可5. 過濾器鏈(配置多個過濾器)* 執行順序:如果有兩個過濾器:過濾器1和過濾器21. 過濾器12. 過濾器23. 資源執行4. 過濾器25. 過濾器1 * 過濾器先后順序問題:1. 注解配置:按照類名的字符串比較規則比較,值小的先執行* 如: AFilter 和 BFilter,AFilter就先執行了。2. web.xml配置: <filter-mapping>誰定義在上邊,誰先執行 4. 案例:1. 案例1_登錄驗證* 需求:1. 訪問day17_case案例的資源。驗證其是否登錄2. 如果登錄了,則直接放行。3. 如果沒有登錄,則跳轉到登錄頁面,提示"您尚未登錄,請先登錄"。 2. 案例2_敏感詞匯過濾* 需求:1. 對day17_case案例錄入的數據進行敏感詞匯過濾2. 敏感詞匯參考《敏感詞匯.txt》3. 如果是敏感詞匯,替換為 *** * 分析:1. 對request對象進行增強。增強獲取參數相關方法2. 放行。傳遞代理對象 * 增強對象的功能:* 設計模式:一些通用的解決固定問題的方式1. 裝飾模式2. 代理模式* 概念:1. 真實對象:被代理的對象2. 代理對象:3. 代理模式:代理對象代理真實對象,達到增強真實對象功能的目的* 實現方式:1. 靜態代理:有一個類文件描述代理模式2. 動態代理:在內存中形成代理類* 實現步驟:1. 代理對象和真實對象實現相同的接口2. 代理對象 = Proxy.newProxyInstance();3. 使用代理對象調用方法。4. 增強方法* 增強方式:1. 增強參數列表2. 增強返回值類型3. 增強方法體執行邏輯 package cn.zep.web.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy;public class ProxyTest {public static void main(String[] args) {// 1. 創建真實對象Lenovo lenovo = new Lenovo();// 2. 動態代理增強lenovo對象/*三個參數:1.類加載器:真實對象.getClass().getClassLoader()2.接口數組:真實對象.getClass().getInterfaces()3.處理器:new InvocationHandler()*/SaleComputer proxy_lenovo = (SaleComputer)Proxy.newProxyInstance(lenovo.getClass().getClassLoader(), lenovo.getClass().getInterfaces(), new InvocationHandler() {/*** 代理邏輯編寫的方法,代理對象調用的所有方法都會觸發該方法執行* @param proxy 代理對象* @param method 代理對象調用的方法被封裝為的對象* @param args 代理對象調用方法時,傳遞的實際參數* @return* @throws Throwable*/@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {/* System.out.println("該方法執行了。。。。");System.out.println(method.getName());System.out.println(args[0]);*/// 判斷是否是sale方法if (method.getName().equals("sale")) {// 1.增強參數double money = (double) args[0];money = money * 0.85;// 3.增強方法體System.out.println("專車接");// 使用真實對象調用該方法String obj = (String) method.invoke(lenovo, money);System.out.println("免費送貨");// 2.增強返回值return obj + "_鼠標墊";}else {// 使用真實對象調用該方法dObject obj = method.invoke(lenovo, args);return obj;}}});// 2. 調用方法String computer = proxy_lenovo.sale(8000);System.out.println(computer); // proxy_lenovo.show();} }
Listener:監聽器
* 概念:web的三大組件之一。* 事件監聽機制* 事件 :一件事情* 事件源 :事件發生的地方* 監聽器 :一個對象* 注冊監聽:將事件、事件源、監聽器綁定在一起。 當事件源上發生某個事件后,執行監聽器代碼* ServletContextListener:監聽ServletContext對象的創建和銷毀* 方法:* void contextDestroyed(ServletContextEvent sce) :ServletContext對象被銷毀之前會調用該方法* void contextInitialized(ServletContextEvent sce) :ServletContext對象創建后會調用該方法* 步驟:1. 定義一個類,實現ServletContextListener接口2. 復寫方法3. 配置1. web.xml<listener><listener-class>cn.itcast.web.listener.ContextLoaderListener</listener-class></listener>* 指定初始化參數<context-param>2. 注解:* @WebListener
web.xml版:
ContextLoaderListener.java:
package cn.zep.web.listener;import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;public class ContextLoaderListener implements ServletContextListener {/**** 監聽ServletContext對象創建的,ServletContext對象在服務器啟動后自動創建* 在服務器啟動后自動調用* @param servletContextEvent*/@Overridepublic void contextInitialized(ServletContextEvent servletContextEvent) {System.out.println("ServletContext對象被創建了。。。");}/**** 在服務器關閉后,ServletContext對象被銷毀。當服務器正常關閉后,該方法被執行調用* @param servletContextEvent*/@Overridepublic void contextDestroyed(ServletContextEvent servletContextEvent) {System.out.println("ServletContext對象被銷毀了。。。");} }
注解版:
ContextLoaderListener.java:
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!-- <filter><filter-name>demo2</filter-name><filter-class>cn.zep.web.filter.FilterDemo2</filter-class></filter><filter-mapping><filter-name>demo2</filter-name><url-pattern>/*</url-pattern></filter-mapping>--><!--配置監聽器--> <!-- <listener><listener-class>cn.zep.web.listener.ContextLoaderListener</listener-class></listener>--><!--指定初始化參數--><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/applicationContext.xml</param-value></context-param></web-app>總結
以上是生活随笔為你收集整理的六、Web服务器——FilterListener 学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十、简单线性回归的python实现(详解
- 下一篇: 十一、MySQL视图学习笔记(详解)