JavaEE基础(05):过滤器、监听器、拦截器,应用详解
本文源碼:GitHub·點這里 || GitEE·點這里
一、Listener監(jiān)聽器
1、概念簡介
JavaWeb三大組件:Servlet,Listener,Filter。監(jiān)聽器就是指在應(yīng)用程序中監(jiān)聽相關(guān)對象狀態(tài)變化的組件。
2、事件源對象
指被監(jiān)聽對象。
- ServletContext
ServletContextListener生命周期監(jiān)聽,它有兩個方法,出生時調(diào)用contextInitialized(),銷毀時調(diào)用contextDestroyed();
ServletContextAttributeListener屬性監(jiān)聽,它有三個方法,添加屬性attributeAdded(),替換屬性attributeReplaced(),移除屬性時attributeRemoved()。
- HttpSession
HttpSessionListener生命周期監(jiān)聽:它有兩個方法,出生時調(diào)用sessionCreated(),銷毀時調(diào)用sessionDestroyed();
HttpSessioniAttributeListener屬性監(jiān)聽:它有三個方法,添加屬性attributeAdded(),替換屬性attributeReplaced(),移除屬性attributeRemoved()。
- ServletRequest
ServletRequestListener生命周期監(jiān)聽:它有兩個方法,出生時調(diào)用requestInitialized(),銷毀時調(diào)用requestDestroyed();
ServletRequestAttributeListener屬性監(jiān)聽:它有三個方法,添加屬性attributeAdded(),替換屬性attributeReplaced(),移除屬性attributeRemoved()。
3、編碼案例
- 相關(guān)監(jiān)聽器
TheContextListener
public class TheContextListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent servletContextEvent) {System.out.println("初始化:TheContextListener");ServletContext servletContext = servletContextEvent.getServletContext() ;servletContext.setAttribute("author","cicada");}@Overridepublic void contextDestroyed(ServletContextEvent servletContextEvent) {System.out.println("銷毀:TheContextListener");} }TheRequestListener
public class TheRequestListener implements ServletRequestListener {@Overridepublic void requestDestroyed(ServletRequestEvent servletRequestEvent) {System.out.println("初始化:TheRequestListener");}@Overridepublic void requestInitialized(ServletRequestEvent servletRequestEvent) {System.out.println("銷毀:TheRequestListener");} }TheSessionListener
public class TheSessionListener implements HttpSessionListener {@Overridepublic void sessionCreated(HttpSessionEvent httpSessionEvent) {System.out.println("初始化:TheSessionListener");}@Overridepublic void sessionDestroyed(HttpSessionEvent httpSessionEvent) {System.out.println("銷毀:TheSessionListener");} }RequestAttributeListener
public class RequestAttributeListener implements ServletRequestAttributeListener {@Overridepublic void attributeAdded(ServletRequestAttributeEvent evt) {System.out.println("Request添加屬性:"+evt.getName()+";"+evt.getValue());}@Overridepublic void attributeRemoved(ServletRequestAttributeEvent evt) {System.out.println("Request移除屬性:"+evt.getName()+";"+evt.getValue());}@Overridepublic void attributeReplaced(ServletRequestAttributeEvent evt) {System.out.println("Request替換屬性:"+evt.getName()+";"+evt.getValue());} }- web.xml配置文件
- 測試接口
二、Filter過濾器
1、過濾器簡介
客戶端請求Servlet時,先執(zhí)行相關(guān)Filter,如果Filter通過,則繼承執(zhí)行請求的Servlet;如果Filter不通過,則不會執(zhí)行用戶請求的Servlet。過濾器可以動態(tài)地攔截請求和響應(yīng)。
2、Filter接口
Filter接口定義了三個核心方法。
- init()
應(yīng)用程序啟動時,服務(wù)器實例化Filter對象,并調(diào)用其init方法,讀取web.xml配置,完成對象的初始化加載。
- doFilter()
實際的過濾操作,請求達到服務(wù)器時,Servlet容器將先調(diào)用過濾器的doFilter方法。
- destroy()
容器在銷毀過濾器前調(diào)用該方法,釋放過濾器占用的資源。
3、編碼案例
- 編寫過濾器
- web.xml配置文件
- 測試接口
三、Interceptor攔截器
Spring框架中的攔截器Interceptor類似于Servlet中的過濾器Filter,主要用于攔截用戶請求并作相應(yīng)的處理。例如通過攔截器可以進行權(quán)限驗證、記錄請求信息的日志、判斷用戶是否登錄等。請求轉(zhuǎn)發(fā)不執(zhí)行攔截、過濾;重定向執(zhí)行攔截和過濾。
四、源代碼地址
GitHub·地址 https://github.com/cicadasmile/java-base-parent GitEE·地址 https://gitee.com/cicadasmile/java-base-parent 新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎!定制產(chǎn)品紅包拿不停!總結(jié)
以上是生活随笔為你收集整理的JavaEE基础(05):过滤器、监听器、拦截器,应用详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自己盲目思考,不如看看经典方案
- 下一篇: MSDN Magazine推出Custo