新聞中心
Servlet監(jiān)聽器監(jiān)聽器概述

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、安平網(wǎng)絡(luò)推廣、微信小程序、安平網(wǎng)絡(luò)營銷、安平企業(yè)策劃、安平品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供安平建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
◆Listener是Servlet的監(jiān)聽器
◆可以監(jiān)聽客戶端的請(qǐng)求、服務(wù)端的操作等。
◆通過監(jiān)聽器,可以自動(dòng)激發(fā)一些操作,如監(jiān)聽在線用戶數(shù)量,當(dāng)增加一個(gè)HttpSession時(shí),給在線人數(shù)加1。
◆編寫監(jiān)聽器需要實(shí)現(xiàn)相應(yīng)的接口
◆編寫完成后在web.xml文件中配置一下,就可以起作用了
◆可以在不修改現(xiàn)有系統(tǒng)基礎(chǔ)上,增加web應(yīng)用程序生命周期事件的跟蹤
常用的Servlet監(jiān)聽器監(jiān)聽接口
◆ServletContextAttributeListener
監(jiān)聽對(duì)ServletContext屬性的操作,比如增加/刪除/修改
◆ServletContextListener
監(jiān)聽ServletContext,當(dāng)創(chuàng)建ServletContext時(shí),激發(fā)contextInitialized(ServletContextEvent sce)方法;當(dāng)銷毀ServletContext時(shí),激發(fā)contextDestroyed(ServletContextEvent sce)方法。
◆HttpSessionListener
監(jiān)聽HttpSession的操作。當(dāng)創(chuàng)建一個(gè)Session時(shí),激發(fā)session Created(SessionEvent se)方法;當(dāng)銷毀一個(gè)Session
時(shí),激發(fā)sessionDestroyed (HttpSessionEvent se)方法。
◆HttpSessionAttributeListener
監(jiān)聽HttpSession中的屬性的操作。當(dāng)在Session增加一個(gè)屬性時(shí),激發(fā)attributeAdded(HttpSessionBindingEvent se) 方法;當(dāng)在Session刪除一個(gè)屬性時(shí),激發(fā)attributeRemoved(HttpSessionBindingEvent se)方法;當(dāng)在Session屬性被重新設(shè)置時(shí),激發(fā)attributeReplaced(HttpSessionBindingEvent se) 方法。
使用范例:
由Servlet監(jiān)聽器管理共享數(shù)據(jù)庫連接
生命周期事件的一個(gè)實(shí)際應(yīng)用由context監(jiān)聽器管理共享數(shù)據(jù)庫連接。在web.xml中如下定義監(jiān)聽器:
- ﹤listener﹥
- ﹤listener-class﹥XXX.MyConnectionManager﹤/listener-class﹥
- ﹤/listener﹥
server創(chuàng)建監(jiān)聽器的實(shí)例,接受事件并自動(dòng)判斷實(shí)現(xiàn)監(jiān)聽器接口的類型。要記住的是由于監(jiān)聽器是配置在部署描述符web.xml中,所以不需要改變?nèi)魏未a就可以添加新的監(jiān)聽器。
- public class MyConnectionManager implements ServletContextListener{
- public void contextInitialized(ServletContextEvent e) {
- Connection con = // create connection
- e.getServletContext().setAttribute("con", con);
- }
- public void contextDestroyed(ServletContextEvent e) {
- Connection con = (Connection) e.getServletContext().getAttribute("con");
- try {
- con.close();
- }
- catch (SQLException ignored) { } // close connection
- }
- }
Servlet監(jiān)聽器保證每新生成一個(gè)servlet context都會(huì)有一個(gè)可用的數(shù)據(jù)庫連接,并且所有的連接對(duì)會(huì)在context關(guān)閉的時(shí)候隨之關(guān)閉。
計(jì)算在線用戶數(shù)量的Linstener
(1)Package xxx;
- public class OnlineCounter {
- private static long online = 0;
- public static long getOnline(){
- return online;
- }
- public static void raise(){
- online++;
- }
- public static void reduce(){
- online--;
- }
- }
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
- public class OnlineCounterListener implements HttpSessionListener{
- public void sessionCreated(HttpSessionEvent hse) {
- OnlineCounter.raise();
- }
- public void sessionDestroyed(HttpSessionEvent hse){
- OnlineCounter.reduce();
- }
- }
在需要顯示在線人數(shù)的JSP中可是使用目前在線人數(shù):
- ﹤%@ page import=“xxx.OnlineCounter" %﹥
- ﹤%=OnlineCounter.getOnline()%﹥
退出會(huì)話(可以給用戶提供一個(gè)注銷按鈕):
- ﹤form action="exit.jsp" method=post﹥
- ﹤input type=submit value="exit"﹥
- ﹤/form﹥
exit.jsp: ﹤%session.invalidate() ;%﹥
在web.xml中加入:
- ﹤listener﹥
- ﹤listener-class﹥servletlistener111111.SecondListener﹤/listener-class﹥
- ﹤/listener﹥
Servlet監(jiān)聽器怎么樣,就是這么簡(jiǎn)單,不用對(duì)現(xiàn)有代碼做任何的修改。趕緊試試吧!
本文標(biāo)題:Servlet監(jiān)聽器概念特點(diǎn)常用概述
URL分享:http://www.dlmjj.cn/article/cdodeeg.html


咨詢
建站咨詢
