事件、監(jiān)聽器

HttpServletRequest婆硬、HttpSession扰才、ServletContext的生命周期都有對應的監(jiān)聽器監(jiān)聽

ServletContextListener

public interface ServletContextListener extends EventListener {
    void contextInitialized(ServletContextEvent var1);

    void contextDestroyed(ServletContextEvent var1);
}
在web容器啟動和關閉時谊惭,會調(diào)用contextInitialized()和contextDestroyed()方法
/**
 * 自定義ServletContextListener叹话,在應用程序啟動加載過程中朝墩,可以做數(shù)據(jù)庫連接醉拓、配置某些參數(shù)操作等
 */
@WebListener
public class DIYServletContextListener implements ServletContextListener{

    public DIYServletContextListener() {
    }

    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();

        // 將妹紙放入context中,在web容器關閉之前收苏,都可以通過context獲取妹紙對象
        // 如果想主動移除設置的妹紙亿卤,則可以context.removeAttribute()
        context.setAttribute("meizhi",new Meizhi("meimeida", 17));

        // 獲取Context的初始化參數(shù),WebListener注解沒有設置初始化參數(shù)的功能鹿霸,只能在web.xml中設置
        String contextInitVal = context.getInitParameter("contextInitKey");

        // 修改tomcat容器對每次會話的設置的ID排吴,默認為JSESSIONID
        SessionCookieConfig scconfig = context.getSessionCookieConfig();
        scconfig.setName("NOT-JSESSIONID");
    }

    public void contextDestroyed(ServletContextEvent sce) {
    }

}
在web.xml中自定義ServletContext的初始化參數(shù)
<context-param>
    <param-name>contextInitKey</param-name>
    <param-value>下期大樂透中獎號碼:235643215</param-value>
</context-param>
除了配置@WebListener注解,也可以在web部署描述符中配置懦鼠,如下:
<listener>
    <listener-class>DIYServletContextListener</listener-class>
</listener>

ServletContextAttributeListener

環(huán)境上下文屬性監(jiān)聽器钻哩,監(jiān)聽上下文屬性的新增、移除和替換
public interface ServletContextAttributeListener extends EventListener {
    void attributeAdded(ServletContextAttributeEvent var1);

    void attributeRemoved(ServletContextAttributeEvent var1);

    void attributeReplaced(ServletContextAttributeEvent var1);
}
當在ServletContext中添加屬性肛冶、移除屬性或替換屬性街氢,對應著attributeAdded()、attributeRemoved()睦袖、attributeReplaced()方法
@WebListener
public class DIYServletContextAttributeListener implements ServletContextAttributeListener {

    @Override
    public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
        // 獲取新增的屬性名稱和屬性值
        servletContextAttributeEvent.getName();
        servletContextAttributeEvent.getValue();
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
        // servletContext移除屬性時珊肃,做的一些操作
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
        // servletContext替換屬性時,做的一些操作
    }
}

HttpSessionListener

HttpSession生命周期監(jiān)聽器馅笙,關注著HttpSession的創(chuàng)建和回收
public interface HttpSessionListener extends EventListener {
    void sessionCreated(HttpSessionEvent var1);

    void sessionDestroyed(HttpSessionEvent var1);
}
可以通過HttpSessionEvent獲取當前HttpSession伦乔,可以統(tǒng)計在線人數(shù)等
@WebListener
public class DIYHttpSessionListener implements HttpSessionListener{
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        // 獲取剛創(chuàng)建好的HttpSession,進行一些操作
        httpSessionEvent.getSession();
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {

    }
}

HttpSessionAttributeListener

public interface HttpSessionAttributeListener extends EventListener {
    void attributeAdded(HttpSessionBindingEvent var1);

    void attributeRemoved(HttpSessionBindingEvent var1);

    void attributeReplaced(HttpSessionBindingEvent var1);
}
監(jiān)聽著HttpSession的屬性新增延蟹、移除评矩、替換等操作叶堆,可以通過HttpSessionBindingEvent獲取到新增阱飘、移除或替換的屬性名和屬性值

HttpSessionBindingListener

實現(xiàn)該監(jiān)聽器無需做@WebListener注解配置或web.xml配置,但在使用的時候需要該監(jiān)聽器的實例,而HttpSessionListener只需要注解配置沥匈、web.xml配置就可以監(jiān)聽整個應用的所有session
public interface HttpSessionBindingListener extends EventListener {
    void valueBound(HttpSessionBindingEvent var1);

    void valueUnbound(HttpSessionBindingEvent var1);
}
當session做setAttribute()時蔗喂,則調(diào)用valueBound方法,需要傳入自身監(jiān)聽器的實例高帖;當session做removeAttribute()時缰儿,則調(diào)用valueUnbound()方法
// 調(diào)用httpSessionBindingListener的valueBound()
session.setAttribute("diy-session-binding-listener", new DIYHttpSessionBindingListener());
// 調(diào)用httpSessionBindingListener的valueUnbound()
session.removeAttribute("diy-session-binding-listener");

HttpSessionActivationListener

@WebListener
public class DIYHttpSessionActivationListener implements HttpSessionActivationListener, Serializable {

    @Override
    public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {
        // 將session從內(nèi)存持久化到磁盤時,會調(diào)用此方法
    }

    @Override
    public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
        // 將session從磁盤反序列化到內(nèi)存時散址,會調(diào)用此方法
    }
}
Servlet規(guī)范不要求一個web容器支持分布式應用乖阵,但是他一定要支持HttpSessionActivationListener接口,以使代碼可以支持分布式環(huán)境预麸。

ServletRequestListener

監(jiān)聽請求對象的創(chuàng)建和回收
public interface ServletRequestListener extends EventListener {
    void requestDestroyed(ServletRequestEvent var1);

    void requestInitialized(ServletRequestEvent var1);
}
請求的生命周期比較短瞪浸,所以兩個方法總是“同時”的執(zhí)行
@WebListener
public class DIYServletRequestListener implements ServletRequestListener {
    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {

    }

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {
        HttpServletRequest request = (HttpServletRequest) servletRequestEvent.getServletRequest();
        request.setAttribute("233",233);
    }
}

ServletRequestAttributeListener

請求屬性變化的監(jiān)聽器,沒啥好說的
public interface ServletRequestAttributeListener extends EventListener {
    void attributeAdded(ServletRequestAttributeEvent var1);

    void attributeRemoved(ServletRequestAttributeEvent var1);

    void attributeReplaced(ServletRequestAttributeEvent var1);
}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吏祸,一起剝皮案震驚了整個濱河市对蒲,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌贡翘,老刑警劉巖蹈矮,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異鸣驱,居然都是意外死亡泛鸟,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門丐巫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谈况,“玉大人,你說我怎么就攤上這事递胧”希” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵缎脾,是天一觀的道長祝闻。 經(jīng)常有香客問我,道長遗菠,這世上最難降的妖魔是什么联喘? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮辙纬,結(jié)果婚禮上豁遭,老公的妹妹穿的比我還像新娘。我一直安慰自己贺拣,他們只是感情好蓖谢,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布捂蕴。 她就那樣靜靜地躺著,像睡著了一般闪幽。 火紅的嫁衣襯著肌膚如雪啥辨。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天盯腌,我揣著相機與錄音溉知,去河邊找鬼。 笑死腕够,一個胖子當著我的面吹牛级乍,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播帚湘,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼卡者,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了客们?” 一聲冷哼從身側(cè)響起崇决,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎底挫,沒想到半個月后恒傻,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡建邓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年盈厘,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片官边。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡沸手,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出注簿,到底是詐尸還是另有隱情契吉,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布诡渴,位于F島的核電站捐晶,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏妄辩。R本人自食惡果不足惜惑灵,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望眼耀。 院中可真熱鬧英支,春花似錦、人聲如沸哮伟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至把敢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間谅辣,已是汗流浹背修赞。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留桑阶,地道東北人柏副。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像蚣录,于是被迫代替她去往敵國和親割择。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容