對于WEB應用來(lái)說(shuō),過(guò)濾器是一個(gè)駐留在服務(wù)器中的WEB組件,他可以截取客戶(hù)端和WEB資源之間的請求和響應信息。
在一個(gè)WEB應用中可以部署多個(gè)過(guò)濾器,多個(gè)過(guò)濾器就組成了一個(gè)過(guò)濾器鏈,請求和響應必須在經(jīng)過(guò)多個(gè)過(guò)濾器后才能到達目標
當配置多個(gè)Filter以后就有一個(gè)執行順序的問(wèn)題,實(shí)際執行順序是按照在web.xml文件中servlet-mapping的順序決定的,如果順序越靠前越先被調用。

作用:1.檢查用戶(hù)訪(fǎng)問(wèn)權限
doFilter(ServletRequest,ServletResponse,FilterChain)作用和service()方法類(lèi)似,是過(guò)濾請求和響應的主要方法。
getFilterName() 獲取Filter的名字
getServletContext() 獲取ServletContext對象(即application)
getInitParameter(String) 獲取Filter的初始化參數
web.xml文件中的Filter配置
<filter-mapping> <!-- Filter的名字 --> <filter-name>Filter1</filter-name> <!-- Filter1的過(guò)濾地址,表示過(guò)濾http://127.0.0.1:8080/day17/admin/user.jsp--> <url-pattern>/admin/user.jsp</url-pattern></filter-mapping>
除此之外在filter-mapping還有一個(gè)子標簽dispatcher,該標簽用來(lái)指定需要Filter處理的請求類(lèi)型
<!-- 用戶(hù)直接訪(fǎng)問(wèn)資源時(shí),會(huì )調用Filter --><dispatcher>REQUEST</dispatcher><!-- 通過(guò)轉發(fā)訪(fǎng)問(wèn)時(shí),會(huì )調用Filter --><dispatcher>FORWARD</dispatcher><!-- 通過(guò)動(dòng)態(tài)包含獲取時(shí),會(huì )調用Filter --><dispatcher>INCLUDE</dispatcher><!-- 當通過(guò)異常處理訪(fǎng)問(wèn)頁(yè)面時(shí),會(huì )調用Filter --><dispatcher>ERROR</dispatcher>
這四種情況可以設置一個(gè),也可以同時(shí)設置多個(gè),如果不設置那么默認為REQUEST
然后執行Filter的init()方法,對象創(chuàng )建后,馬上就被調用,對Filter做一些初始化操作
執行Filter的doFilter()方法,每次訪(fǎng)問(wèn)目標資源,只要匹配過(guò)濾的地址,就會(huì )調用。
獲取Filter在web.xml文件中配置的名稱(chēng)
獲取Filter在web.xml文件中配置的初始化參數
// 獲取Filter的名稱(chēng)String filterName = filterConfig.getFilterName();// 獲取初始化參數。username的值String username = filterConfig.getInitParameter("username");// 獲取ServletContext的對象實(shí)例 ServletContext ctx = filterConfig.getServletContext();
一般Filter.doFilter中的代碼分為三段:
第一段是FilterChain.doFilter之前的代碼。一般用來(lái)做請求的攔截,檢查用戶(hù)訪(fǎng)問(wèn)的權限,訪(fǎng)問(wèn)日記的記錄。參數編碼的設置等等操作。
第二段是FilterChain.doFilter方法。此方法可以將代碼的執行傳遞到下一個(gè)Filter中?;蛘呤莻鬟f到用戶(hù)最終訪(fǎng)問(wèn)的資源中。
第三段是FilterChain.doFilter之后的代碼。主要用過(guò)做一些日志操作。我們很少會(huì )在第三段中做太多復雜的操作。
主要有以下兩種
精確匹配:/路徑/資源名
比如:/index.html、/hello/index.jsp 、 /client/LoginServlet 等,只有在請求地址完全一樣時(shí)才會(huì )調用Filter
目錄匹配:/路徑名/*
比如1:/abc/* 表示可以攔截abc目錄下的所有資源,甚至是abc目錄下的其他目錄。
比如2:/* 表示只要訪(fǎng)問(wèn)項目根目錄下的資源就會(huì )調用Filter
后綴名匹配:*.后綴名
比如:*.jsp 表示攔截所有后綴為jsp文件資源
第二種:通過(guò)filter-mapping中的servlet-name來(lái)指定要過(guò)濾的Servlet
在Filter的filter-mapping中增加了一個(gè)servlet-name標簽,將該標簽的值設置成Servlet的名字,在訪(fǎng)問(wèn)Servlet時(shí)就會(huì )調用該過(guò)濾器過(guò)濾請求。
<filter-mapping> <filter-name>HelloFilter</filter-name> <servlet-name>HelloServlet</servlet-name> </filter-mapping>
Listener用于監聽(tīng)JavaWeb程序中的事件,當事件被觸發(fā)時(shí),監聽(tīng)器中的指定方法將會(huì )被調用。
作用:監聽(tīng)ServletContext對象的創(chuàng )建與銷(xiāo)毀
方法:
public void contextInitialized ( ServletContextEvent sce ):ServletContext創(chuàng )建時(shí)調用
public void contextDestroyed ( ServletContextEvent sce ):ServletContext銷(xiāo)毀時(shí)調用
ServletContextEvent對象
作用:public ServletContext getServletContext ():獲取ServletContext對象
HttpSessionListener
作用:監聽(tīng)HttpSession對象的創(chuàng )建與銷(xiāo)毀
方法:
public void sessionCreated ( HttpSessionEvent se ):HttpSession對象創(chuàng )建時(shí)調用
public void sessionDestroyed ( HttpSessionEvent se ):HttpSession對象銷(xiāo)毀時(shí)調用
HttpSessionEvent對象
作用:public HttpSession getSession ():獲取當前HttpSession對象
ServletRequestListener
作用:監聽(tīng)ServletRequest對象的創(chuàng )建與銷(xiāo)毀
方法:
public void requestInitialized ( ServletRequestEvent sre ):ServletRequest對象創(chuàng )建時(shí)調用
public void requestDestroyed ( ServletRequestEvent sre ):ServletRequest對象銷(xiāo)毀時(shí)調用
ServletRequestEvent對象
作用:
public ServletRequest getServletRequest ():獲取當前的ServletRequest對象。
三種創(chuàng )建與銷(xiāo)毀的監聽(tīng)器使用起來(lái)基本一致。
在web.xml文件中注冊監聽(tīng)器
<listener><listener-class>com.web.listener.MyServletContextListener</listener-class></listener>
作用:監聽(tīng)ServletContext中屬性的創(chuàng )建、修改和銷(xiāo)毀
方法:
public void attributeAdded(ServletContextAttributeEvent scab):向ServletContext中添加屬性時(shí)調用
public void attributeRemoved(ServletContextAttributeEvent scab):從ServletContext中移除屬性時(shí)調用
public void attributeReplaced(ServletContextAttributeEvent scab):當ServletContext中的屬性被修改時(shí)調用
ServletContextAttributeEvent對象
作用:
public String getName() :獲取修改或添加的屬性名
public Object getValue():獲取被修改或添加的屬性值
public ServletContext getServletContext ():獲取當前WEB應用的ServletContext對象
HttpSessionAttributeListener
作用:監聽(tīng)HttpSession中屬性的創(chuàng )建、修改和銷(xiāo)毀
方法:
public void attributeAdded ( HttpSessionBindingEvent se ):向HttpSession中添加屬性時(shí)調用
public void attributeRemoved(HttpSessionBindingEvent se):從HttpSession中移除屬性時(shí)調用
public void attributeReplaced(HttpSessionBindingEvent se):當HttpSession中的屬性被修改時(shí)調用
HttpSessionBindingEvent對象
作用:
public String getName() :獲取修改或添加的屬性名
public Object getValue():獲取被修改或添加的屬性值
public HttpSession getSession ():獲取當前的HttpSession對象
ServletRequestAttributeListener
作用:監聽(tīng)ServletRequest中屬性的創(chuàng )建、修改和銷(xiāo)毀
方法:
public void attributeAdded (ServletRequestAttributeEvent srae ):向ServletRequest中添加屬性時(shí)調用
public void attributeRemoved(ServletRequestAttributeEvent srae):從ServletRequest中移除屬性時(shí)調用
public void attributeReplaced(ServletRequestAttributeEvent srae):當ServletRequest中的屬性被修改時(shí)調用
ServletRequestAttributeEvent對象
作用:
public String getName():獲取修改或添加的屬性名
public Object getValue():獲取被修改或添加的屬性值
作用:監聽(tīng)某個(gè)對象在session域中的創(chuàng )建與移除。
方法:
public void valueBound(HttpSessionBindingEvent event):該類(lèi)的實(shí)例被放到Session域中時(shí)調用
public void valueUnbound(HttpSessionBindingEvent event):該類(lèi)的實(shí)例從Session中移除時(shí)調用
HttpSessionBindingEvent對象
作用:
public HttpSession getSession ():獲取HttpSession對象
public String getName():獲取操作的屬性名
public Object getValue():獲取操作的屬性值
作用:監聽(tīng)某個(gè)對象在session中的序列化與反序列化。
方法:
public void sessionWillPassivate(HttpSessionEvent se):該類(lèi)實(shí)例和Session一起鈍化到硬盤(pán)時(shí)調用
public void sessionDidActivate(HttpSessionEvent se):該類(lèi)實(shí)例和Session一起活化到內存時(shí)調用
HttpSessionEvent對象
作用:
public HttpSession getSession ():獲取HttpSession對象
注意:為被監聽(tīng)類(lèi)對象可以正常序列化到硬盤(pán)上,還需要讓該類(lèi)實(shí)現java.io.Serializable接口
聯(lián)系客服