一.過濾器Filter
對(duì)指定的web資源進(jìn)行攔截,實(shí)現(xiàn)一些特殊功能,如實(shí)現(xiàn)URL權(quán)限訪問鸦泳、過濾敏感詞等等
1).注冊過濾器
方式一
在WEB-INF/web.xml注冊
<filter>
<filter-name>filter</filter-name>
<filter-class>com.xxx.xxFilter</filter-class>
<init-param>
<param-name>name1</param-name>
<param-value>value1</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern> 指定要攔截的url
<servlet-name>xxxServlet</servlet-name> 指定要攔截的Servlet
<dispatcher>REQUEST</dispatcher> 指定要攔截資源的訪問方式是REQUEST/FORWARD/INCLUDE/ERROR,默認(rèn)REQUEST
</filter-mapping>
方式二
在com.xxx.xxFilter類添加注解
@WebFilter(filterName="filter",urlPatterns={"/*"},servletNames={"Demo1Servlet"})
2).實(shí)現(xiàn)接口
創(chuàng)建: web應(yīng)用加載時(shí),創(chuàng)建所有過濾器對(duì)象,調(diào)用init()初始化,一直駐留內(nèi)存;
銷毀: web應(yīng)用移除時(shí),銷毀所有過濾器對(duì)象,銷毀前調(diào)用destroy()善后
public class xxFilter implements Filter {
public void destroy() {
}
// 每次攔截資源后調(diào)用
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
FilterChain過濾器鏈對(duì)象,一個(gè)資源可能被多個(gè)過濾器所攔截,攔截順序是web.xml中filter-mapping配置順序
FilterChain.doFilter()一旦被調(diào)用,就執(zhí)行過濾器鏈的下一個(gè)節(jié)點(diǎn),若當(dāng)前是最后過濾器,則執(zhí)行目標(biāo)資源
}
public void init(FilterConfig filterConfig) throws ServletException {
filterConfig.getInitParameter(String name) 獲取web.xml過濾器配置信息
filterConfig.getServletContext() 獲取ServletContext對(duì)象
}
}
二.監(jiān)聽器Listener
Servlet監(jiān)聽器: 監(jiān)聽ServletContext、Session和Request域的變化词身。
1.監(jiān)聽三大作用域的創(chuàng)建和銷毀
1)注冊監(jiān)聽器
方式一
在WEB-INF/web.xml注冊
<listener>
<listener-class>com.xxx.xxx</listener-class>
</listener>
方式二
在com.xxx.xxx類添加注解@WebListener
2)實(shí)現(xiàn)監(jiān)聽器接口
public class xxx implements ServletContextListener {
// ServletContext創(chuàng)建: web應(yīng)用被加載時(shí)創(chuàng)建
public void contextDestroyed(ServletContextEvent sce) {
}
// ServletContext銷毀: web應(yīng)用被移除時(shí)銷毀
public void contextInitialized(ServletContextEvent sce) {
}
}
應(yīng)用場景: 保存全局對(duì)象,如:創(chuàng)建數(shù)據(jù)庫連接池; 加載框架配置文件;實(shí)現(xiàn)任務(wù)調(diào)度定時(shí)器
public class xxx implements HttpSessionListener {
// Session創(chuàng)建:首次調(diào)用request.getSession()時(shí)創(chuàng)建
public void sessionCreated(HttpSessionEvent se) {
}
// Session銷毀:超30分鐘沒用時(shí)銷毀 /調(diào)用invalidate()銷毀
// 服務(wù)器非正常關(guān)閉時(shí)銷毀(正常關(guān)閉則被鈍化,再次開啟則被活化)
public void sessionDestroyed(HttpSessionEvent se) {
}
}
public class xxx implements ServletRequestListener {
// Request創(chuàng)建:瀏覽器請(qǐng)求開始時(shí)創(chuàng)建
public void requestDestroyed(ServletRequestEvent sre) {
}
// Request銷毀:瀏覽器請(qǐng)求結(jié)束時(shí)銷毀
public void requestInitialized(ServletRequestEvent sre) {
}
}
2.監(jiān)聽三大作用域?qū)傩缘脑鰟h改
public class xxx implements ServletContextAttributeListener {
public void attributeAdded(ServletContextAttributeEvent scab) {
System.out.println("屬性加入"+scab.getName()+":"+scab.getValue());
}
public void attributeRemoved(ServletContextAttributeEvent scab) {
System.out.println("屬性移除"+scab.getName()+":"+scab.getValue());
}
public void attributeReplaced(ServletContextAttributeEvent scab) {
System.out.println("屬性替換"+scab.getName()+":"+scab.getValue()+
":"+scab.getServletContext().getAttribute(scab.getName()));
}
}
public class xxx implements HttpSessionAttributeListener {
public void attributeAdded(HttpSessionBindingEvent se) {
}
public void attributeRemoved(HttpSessionBindingEvent se) {
}
public void attributeReplaced(HttpSessionBindingEvent se) {
}
}
public class xxx implements ServletRequestAttributeListener {
public void attributeAdded(ServletRequestAttributeEvent srae) {
}
public void attributeRemoved(ServletRequestAttributeEvent srae) {
}
public void attributeReplaced(ServletRequestAttributeEvent srae) {
}
}
3.監(jiān)聽JavaBean對(duì)象在Session域中的變化
public class JavaBean類 implements HttpSessionBindingListener ,HttpSessionActivationListener{
...
// javabean對(duì)象被綁到session
public void valueBound(HttpSessionBindingEvent event) {
}
// javabean對(duì)象被移出session
public void valueUnbound(HttpSessionBindingEvent event) {
}
// javabean對(duì)象隨session活化
public void sessionDidActivate(HttpSessionEvent se) {
}
// javabean對(duì)象隨session鈍化
public void sessionWillPassivate(HttpSessionEvent se) {
}
}
簡書: http://www.reibang.com/p/46e5b3bf42d7
CSDN博客: http://blog.csdn.net/qq_32115439/article/details/54791009
GitHub博客:http://lioil.win/2017/01/30/Servlet-Listener.html
Coding博客:http://c.lioil.win/2017/01/30/Servlet-Listener.html