淺談Servlet技術(shù)中的Listener起到的作用

Listener是在servlet2.3中加入的,主要用于對Session,request,context等進行監(jiān)控。使用Listener需要實現(xiàn)響應(yīng)的接口。觸發(fā)Listener事件的時候,tomcat會自動調(diào)用Listener的方法。在web.xml中配置標(biāo)簽,一般要配置在標(biāo)簽前面,可配置多個,同一種類型也可配置多個com.xxx.xxx.ImplementListenerservlet2.5的規(guī)范中共有8中Listener,分別監(jiān)聽session,context,request等的創(chuàng)建和銷毀,屬性變化等。

常用的監(jiān)聽接口: 監(jiān)聽對象HttpSessionListener :監(jiān)聽HttpSession的操作,監(jiān)聽session的創(chuàng)建和銷毀。 可用于收集在線著信息ServletRequestListener:監(jiān)聽request的創(chuàng)建和銷毀。ServletContextListener:監(jiān)聽context的創(chuàng)建和銷毀。??

啟動時獲取web.xml里配置的初始化參數(shù)監(jiān)聽對象的屬性HttpSessionAttributeListener :ServletContextAttributeListener :ServletRequestAttributeListener :監(jiān)聽session內(nèi)的對象HttpSessionBindingListener:對象被放到session里執(zhí)行valueBound(),對象移除時執(zhí)行valueUnbound()。對象必須實現(xiàn)該lisener接口。HttpSessionActivationListener:服務(wù)器關(guān)閉時,會將session里的內(nèi)容保存到硬盤上,這個過程叫做鈍化。服務(wù)器重啟時,會將session內(nèi)容從硬盤上重新加載。

下面為大家分享一個Listener的應(yīng)用實例:利用HttpSessionListener統(tǒng)計最多在線用戶人數(shù)

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.servlet.

ServletContext;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;public class HttpSessionListenerImpl implements HttpSessionListener {? ? public void sessionCreated(HttpSessionEvent event) {? ? ? ? ServletContext app = event.getSession().getServletContext();? ? ? ? int count = Integer.parseInt(app.getAttribute("onLineCount").toString());? ? ? ? count++;? ? ? ? app.setAttribute("onLineCount", count);? ? ? ? int maxOnLineCount = Integer.parseInt(app.getAttribute("maxOnLineCount").toString());? ? ? ? if (count > maxOnLineCount) {? ? ? ? ? ? //記錄最多人數(shù)是多少? ? ? ? ? ? app.setAttribute("maxOnLineCount", count);? ? ? ? ? ? DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");? ? ? ? ? ? //記錄在那個時刻達到上限? ? ? ? ? ? app.setAttribute("date", df.format(new Date()));? ? ? ? }? ? }? ? //session注銷、超時時候調(diào)用,停止tomcat不會調(diào)用? ? public void sessionDestroyed(HttpSessionEvent event) {? ? ? ? ServletContext app = event.getSession().getServletContext();? ? ? ? int count = Integer.parseInt(app.getAttribute("onLineCount").toString());? ? ? ? count--;? ? ? ? app.setAttribute("onLineCount", count);? ? ? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文包括:1、Listener簡介2、Servlet監(jiān)聽器3、監(jiān)聽三個域?qū)ο髣?chuàng)建和銷毀的事件監(jiān)聽器4、監(jiān)聽三個域?qū)?..
    廖少少閱讀 6,633評論 6 28
  • 監(jiān)聽器(listener) 監(jiān)聽器簡介 :監(jiān)聽器就是一個實現(xiàn)特定接口的普通java程序,這個程序?qū)iT用于監(jiān)聽另一個...
    奮斗的老王閱讀 2,672評論 0 53
  • ServletContext 被 Servlet 程序用來與 Web 容器通信。例如寫日志,轉(zhuǎn)發(fā)請求。每一個 We...
    凱哥學(xué)堂閱讀 1,067評論 0 0
  • 這部分主要是與Java Web和Web Service相關(guān)的面試題。 96、闡述Servlet和CGI的區(qū)別? 答...
    雜貨鋪老板閱讀 1,501評論 0 10
  • 1、過濾器的基本概念 現(xiàn)實中的監(jiān)聽器定義:是指專門用于對其他對象身上發(fā)生的事件或狀態(tài)改變進行監(jiān)聽和相應(yīng)處理的對象,...
    年少懵懂丶流年夢閱讀 403評論 1 2

友情鏈接更多精彩內(nèi)容