Java監(jiān)聽器的作用及用法代碼示例
監(jiān)聽器在JavaWeb開發(fā)中用得比較多
Java Web開發(fā)中的監(jiān)聽器(listener)就是application、session、request三個對象創(chuàng)建、銷毀或者往其中添加修改刪除屬性時自動執(zhí)行代碼的功能組件,如下所示:
①ServletContextListener:對Servlet上下文的創(chuàng)建和銷毀進(jìn)行監(jiān)聽。
②ServletContextAttributeListener:監(jiān)聽Servlet上下文屬性的添加、刪除和替換。
③HttpSessionListener:對Session的創(chuàng)建和銷毀進(jìn)行監(jiān)聽。
補(bǔ)充:session的銷毀有兩種情況:1). session超時(可以在web.xml中通過<session-config>/<session-timeout>標(biāo)簽配置超時時間);2). 通過調(diào)用session對象的invalidate()方法使session失效。
④HttpSessionAttributeListener:對Session對象中屬性的添加、刪除和替換進(jìn)行監(jiān)聽。
⑤ServletRequestListener:對請求對象的初始化和銷毀進(jìn)行監(jiān)聽。
⑥ServletRequestAttributeListener:對請求對象屬性的添加、刪除和替換進(jìn)行監(jiān)聽。
監(jiān)聽器常用的用途
它可以監(jiān)聽客戶端的請求、服務(wù)端的操作等。通過監(jiān)聽器,可以自動激發(fā)一些操作,比如監(jiān)聽在線的用戶的數(shù)量
通常使用Web監(jiān)聽器做以下的內(nèi)容:
統(tǒng)計(jì)在線人數(shù),利用HttpSessionLisener
加載初始化信息:利用ServletContextListener
統(tǒng)計(jì)網(wǎng)站訪問量
實(shí)現(xiàn)訪問監(jiān)控
有時候后臺需要統(tǒng)計(jì)最多在線人數(shù)以及當(dāng)前在線人數(shù),這里通過監(jiān)聽器實(shí)現(xiàn)這一功能。
圖片僅供參考。。。
實(shí)例一
下面是一個統(tǒng)計(jì)網(wǎng)站最多在線人數(shù)監(jiān)聽器的例子
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; /** 上下文監(jiān)聽器,在服務(wù)器啟動時初始化onLineCount和maxOnLineCount兩個變量 并將其置于服務(wù)器上下文(ServletContext)中,其初始值都是0 */ @WebListener public class InitListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent evt) { } @Override public void contextInitialized(ServletContextEvent evt) { evt.getServletContext().setAttribute("onLineCount", 0); evt.getServletContext().setAttribute("maxOnLineCount", 0); } }
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletContext; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** 會話監(jiān)聽器,在用戶會話創(chuàng)建和銷毀的時候根據(jù)情況 修改onLineCount和maxOnLineCount的值 */ @WebListener public class MaxCountListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent event) { ServletContext ctx = event.getSession().getServletContext(); int count = Integer.parseInt(ctx.getAttribute("onLineCount").toString()); count++; ctx.setAttribute("onLineCount", count); int maxOnLineCount = Integer.parseInt(ctx.getAttribute("maxOnLineCount").toString()); if (count > maxOnLineCount) { ctx.setAttribute("maxOnLineCount", count); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ctx.setAttribute("date", df.format(new Date())); } } @Override public void sessionDestroyed(HttpSessionEvent event) { ServletContext app = event.getSession().getServletContext(); int count = Integer.parseInt(app.getAttribute("onLineCount").toString()); count--; app.setAttribute("onLineCount", count); } }
說明:這里使用了Servlet 3規(guī)范中的@WebListener注解配置監(jiān)聽器,當(dāng)然你可以在web.xml文件中用<listener>標(biāo)簽配置監(jiān)聽器。
實(shí)例二
在JavaWeb應(yīng)用開發(fā)中,有時候我們需要統(tǒng)計(jì)當(dāng)前在線的用戶數(shù),此時就可以使用監(jiān)聽器技術(shù)來實(shí)現(xiàn)這個功能了。
package me.gacl.web.listener; import javax.servlet.ServletContext; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * @ClassName: OnLineCountListener * @Description: 統(tǒng)計(jì)當(dāng)前在線用戶個數(shù) * @author: 孤傲蒼狼 * @date: 2014-9-10 下午10:01:26 * */ public class OnLineCountListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent se) { ServletContext context = se.getSession().getServletContext(); Integer onLineCount = (Integer) context.getAttribute("onLineCount"); if(onLineCount==null){ context.setAttribute("onLineCount", 1); }else{ onLineCount++; context.setAttribute("onLineCount", onLineCount); } } @Override public void sessionDestroyed(HttpSessionEvent se) { ServletContext context = se.getSession().getServletContext(); Integer onLineCount = (Integer) context.getAttribute("onLineCount"); if(onLineCount==null){ context.setAttribute("onLineCount", 1); }else{ onLineCount--; context.setAttribute("onLineCount", onLineCount); } } }
總結(jié)
以上就是本文關(guān)于Java監(jiān)聽器的作用及用法代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:javaweb設(shè)計(jì)中filter粗粒度權(quán)限控制代碼示例、Javaweb項(xiàng)目session超時解決方案、JavaWeb項(xiàng)目中dll文件動態(tài)加載方法解析(詳細(xì)步驟)等,如有不足之處,歡迎留言指出。感謝朋友們長久以來對本站的支持!
相關(guān)文章
RocketMQ生產(chǎn)者如何規(guī)避故障Broker方式詳解
這篇文章主要為大家介紹了RocketMQ生產(chǎn)者如何規(guī)避故障Broker方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11SpringBoot使用Logback進(jìn)行日志記錄的代碼示例
在開發(fā)Web應(yīng)用程序時,日志記錄是非常重要的一部分,在SpringBoot中,我們可以使用Logback進(jìn)行日志記錄,Logback是一款高性能、靈活的日志框架,它可以滿足各種不同的日志需求,在本文中,我們介紹了如何在SpringBoot中使用Logback進(jìn)行日志記錄2023-06-06解決redisTemplate中l(wèi)eftPushAll隱性bug的問題
這篇文章主要介紹了解決redisTemplate中l(wèi)eftPushAll隱性bug的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02解決出現(xiàn) java.lang.ExceptionInInitializerError錯誤問題
這篇文章主要介紹了解決出現(xiàn) java.lang.ExceptionInInitializerError錯誤問題的相關(guān)資料,需要的朋友可以參考下2017-01-01詳談java編碼互轉(zhuǎn)(application/x-www-form-urlencoded)
下面小編就為大家?guī)硪黄斦刯ava編碼互轉(zhuǎn)(application/x-www-form-urlencoded)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07