Servlet過濾器和監聽器

一、過濾器

實現javax.servlet.Filter接口;
一般客戶端發出請求後會交給Servlet;如果過濾器存在,則客戶端發出的請求都是先交給過濾器,然後交給Servlet;

我們可以完成一些在執行Servlet之前必須要做的事,比如request.setCharacterEncoding("UTF-8");
必須實現以下方法:

  1. public void init(FilterConfig config) throws ServletException{}
  2. public void doFilter(ServletRequest req,ServletResponse resp,FilterChain chain){}
  3. public void destroy(){}

注意:

  1. init方法在Web容器初始化時就會調用;
  2. doFilter的參數是ServletRequestServletResponse而不是Http的;
  3. FilterChain含有public void doFilter(ServletRequest req,ServletResponse resp){}
  4. 一般代碼形式如下:
public void doFilter(ServletRequest req,ServletResponse resp,FilterChain chain){
    chain.doFilter(req,resp);//執行Servlet操作;
}

則這個函數會調用兩次,一次是執行chain.doFilter之前,一次是執行chain.doFilter()之後;
寫完過濾器後,我們必須要限制過濾器調用的範圍,即域名爲多少時會調用過濾器,我們在web.xml 中進行配置;

<filter>
    <filter-name></filter-name>
    <filter-class></filter-class>
</filter>
<filter-mapping>
    <filter-name></filter-name>
    <url-pattern></url-pattern>            <!--過濾器應用的範圍,如果爲/*,則如果域名設置形如/a 或/abc等都會調用過濾器-->
</filter-mapping>

二、監聽器

監聽器的作用類似於Swing中的監聽器的作用,效果也差不多,即當某個事件發生時,就觸發了某個設置好的監聽器,這裏監聽器能監聽applicationsessionrequest對象。
寫好監聽器類後需要配置web.xml,形式如下:

<listener>
    <listener-class></listener-class>
</listener>

1.application監聽器:ServletContextListener

需要實現的方法:
(1)public void contextInitialized(ServletContextEvent e); //在web容器初始化是就調用
(2)public void contextDestroyed(ServletContextEvent e); //當web容器銷燬時調用
ServletContextEvent含有getServletContext()方法取得application對象;

2.application屬性監聽器:ServletContextAttributeListener

需要實現的方法:
(1)public void attributeAdded(ServletContextAttributeEvent e); //當調用application.setAttribute()時調用
(2)public void attributeRemoved(ServletContextAttributeEvent e); //當調用applcaition.removeAttribute()時調用
(3)public void attributeReplaced(ServletContextAttributeEvent e); //當調用兩次application.setAttribute()賦予相同屬性時調用

ServletContextAttributeEvent 的方法有:
(1)getName();
(2)getValue();

3.session監聽器:HttpSessionListener

需要實現的方法:
(1)public void sessionCreated(HttpSessionEvent e); //當調用關於session對象的方法時,比如session.getId()
(2)public void sessionDestroyed(HttpSessionEvent e); //當調用session.invalidate();或超時 時調用
HttpSessionEvent的方法有getSession();

4.session屬性監聽器:HttpSessionAttributeListener

需要實現的方法:
(1)public void attributeAdded(HttpSessionBindingEvent e); //當調用session.setAttribute()時調用
(2)public void attributeRemoved(HttpSessionBindingEvent e); //當調用session.removeAttribute()時調用
(3)public void attributeReplaced(HttpSessionBindingEvent e); //當調用兩次session.setAttribute()賦予相同屬性時調用

HttpSessionBindingEvent方法:
(1)getSession();
(2)getName();
(3)getValue();

5.session屬性綁定監聽器:HttpSessionBindingListener

需要實現的方法:
(1)public void valueBound(HttpSessionBindingEvent e);
(2)public void valueUnbound(HttpSessionBindingEvent e);

注意:這個監聽器不用在web.xml中進行配置,而自動生效。
當實現這個接口的類被作爲屬性添加如內置對象時,就會觸發valueBound;當刪除這個屬性時,則會觸發valueUnbound
比如

class A implements HttpSessionBindingListener{
    .....
    public void valueBound(HttpSessionBindingEvent e){}
    public void valueUnbound(HttpSessionBindingEvent e){}
}

當調用session.setAttribute("info",new A())時即添加A類對象時,則會觸發valueBound方法,當調用session.removeAttribute("info")時觸發valueUnbound方法;

6.request監聽器:ServletRequestListener

需要實現的方法:
(1)public void requestInitialized(ServletRequestEvent e); //當請求一個網頁時會調用
(2)public void requestDestroyed(ServletRequestEvent e); //當請求結束時會調用

ServletRequestEvent 方法:
(1)getServletContext();
(2)getServletRequest();

7.request屬性監聽器:ServletRequestAttributeListener

需要實現的方法:
(1)attributeAdded(ServletRequestAttributeEvent e); //當調用request.setAttribute()時調用
(2)attributeRemoved(ServletRequestAttributeEvent e); //當調用request.removeAttribute()時調用
(3)attributeReplaced(ServletRequestAttributeEvent e); //當調用兩次request.setAttribute()賦予相同屬性時調用

ServletRequestAttributeEvent 方法:
(1)getName();
(2)getValue();

轉自xiazdong

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章