日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

六、Web服务器——FilterListener 学习笔记

發布時間:2024/7/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 六、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();} }



package cn.itcast.web.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.List; /*** 敏感詞匯過濾器*/ @WebFilter("/*") public class SensitiveWordsFilter implements Filter {public void destroy() {}public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {// 1.創建代理對象,增強getParameter方法ServletRequest proxy_req = (ServletRequest) Proxy.newProxyInstance(req.getClass().getClassLoader(), req.getClass().getInterfaces(), new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {//增強getParameter方法// 判斷是否是getParameter方法if (method.getName().equals("getParameter")) {// 增強返回值// 獲取返回值String value = (String) method.invoke(req,args);if (value != null) {for (String str : list) {if (value.contains(str)){value = value.replaceAll(str,"***");}}}return value;}return method.invoke(req,args);}});// 2.放行chain.doFilter(proxy_req, resp);}private List<String> list = new ArrayList<String>(); // 敏感詞匯集合public void init(FilterConfig config) throws ServletException {try {// 1.加載文件,獲取文件的真實路徑ServletContext servletContext = config.getServletContext();String realPath = servletContext.getRealPath("/WEB-INF/classes/敏感詞匯.txt");// 2.讀取文件BufferedReader br = new BufferedReader(new FileReader(realPath));// 3.將文件的每一行數據添加到list中String line = null;while ((line = br.readLine())!= null) {list.add(line);}br.close();System.out.println(list);} catch (Exception e) {e.printStackTrace();}}} package cn.itcast.web.servlet; 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; @WebServlet("/sensitiveServlet") public class SensitiveServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String name = request.getParameter("name");String msg = request.getParameter("msg");System.out.println(name + ":" + msg);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response);} }

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版:

<?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></web-app>

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:

package cn.zep.web.listener;import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import java.io.FileInputStream; import java.io.FileNotFoundException;@WebListener public class ContextLoaderListener implements ServletContextListener {/**** 監聽ServletContext對象創建的,ServletContext對象在服務器啟動后自動創建* 在服務器啟動后自動調用* @param servletContextEvent*/@Overridepublic void contextInitialized(ServletContextEvent servletContextEvent) {// 加載資源文件// 1.獲取ServletContext對象ServletContext servletContext = servletContextEvent.getServletContext();// 2.加載資源文件String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");// 3.獲取真實路徑String realPath = servletContext.getRealPath(contextConfigLocation);// 4.加載進內存try {FileInputStream fis = new FileInputStream(realPath);System.out.println(fis);} catch (FileNotFoundException e) {e.printStackTrace();}System.out.println("ServletContext對象被創建了。。。");}/**** 在服務器關閉后,ServletContext對象被銷毀。當服務器正常關閉后,該方法被執行調用* @param servletContextEvent*/@Overridepublic void contextDestroyed(ServletContextEvent servletContextEvent) {System.out.println("ServletContext對象被銷毀了。。。");} }

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 学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。