日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Servlet監(jiān)聽器概念特點常用概述

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è)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)公司為所有大學生創(chuàng)業(yè)者提供安平建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com

◆Listener是Servlet的監(jiān)聽器

◆可以監(jiān)聽客戶端的請求、服務(wù)端的操作等。

◆通過監(jiān)聽器,可以自動激發(fā)一些操作,如監(jiān)聽在線用戶數(shù)量,當增加一個HttpSession時,給在線人數(shù)加1。

◆編寫監(jiān)聽器需要實現(xiàn)相應(yīng)的接口

◆編寫完成后在web.xml文件中配置一下,就可以起作用了

◆可以在不修改現(xiàn)有系統(tǒng)基礎(chǔ)上,增加web應(yīng)用程序生命周期事件的跟蹤

常用的Servlet監(jiān)聽器監(jiān)聽接口

◆ServletContextAttributeListener

監(jiān)聽對ServletContext屬性的操作,比如增加/刪除/修改

◆ServletContextListener

監(jiān)聽ServletContext,當創(chuàng)建ServletContext時,激發(fā)contextInitialized(ServletContextEvent sce)方法;當銷毀ServletContext時,激發(fā)contextDestroyed(ServletContextEvent sce)方法。

◆HttpSessionListener

監(jiān)聽HttpSession的操作。當創(chuàng)建一個Session時,激發(fā)session Created(SessionEvent se)方法;當銷毀一個Session
時,激發(fā)sessionDestroyed (HttpSessionEvent se)方法。

◆HttpSessionAttributeListener

監(jiān)聽HttpSession中的屬性的操作。當在Session增加一個屬性時,激發(fā)attributeAdded(HttpSessionBindingEvent se) 方法;當在Session刪除一個屬性時,激發(fā)attributeRemoved(HttpSessionBindingEvent se)方法;當在Session屬性被重新設(shè)置時,激發(fā)attributeReplaced(HttpSessionBindingEvent se) 方法。

使用范例:

由Servlet監(jiān)聽器管理共享數(shù)據(jù)庫連接

生命周期事件的一個實際應(yīng)用由context監(jiān)聽器管理共享數(shù)據(jù)庫連接。在web.xml中如下定義監(jiān)聽器:

 
 
 
  1. ﹤listener﹥
  2.     ﹤listener-class﹥XXX.MyConnectionManager﹤/listener-class﹥
  3. ﹤/listener﹥ 

server創(chuàng)建監(jiān)聽器的實例,接受事件并自動判斷實現(xiàn)監(jiān)聽器接口的類型。要記住的是由于監(jiān)聽器是配置在部署描述符web.xml中,所以不需要改變?nèi)魏未a就可以添加新的監(jiān)聽器。

 
 
 
  1. public class MyConnectionManager implements ServletContextListener{  
  2.   public void contextInitialized(ServletContextEvent e) { 
  3.         Connection con = // create connection 
  4.         e.getServletContext().setAttribute("con", con); 
  5.     }  
  6.    public void contextDestroyed(ServletContextEvent e) { 
  7.         Connection con = (Connection) e.getServletContext().getAttribute("con"); 
  8.         try {
  9.           con.close(); 
  10.         } 
  11.        catch (SQLException ignored) { } // close connection 
  12.     } 
  13. }  

Servlet監(jiān)聽器保證每新生成一個servlet context都會有一個可用的數(shù)據(jù)庫連接,并且所有的連接對會在context關(guān)閉的時候隨之關(guān)閉。

計算在線用戶數(shù)量的Linstener

(1)Package xxx;

 
 
 
  1. public class OnlineCounter {  
  2.    private static long online = 0;     
  3.    public static long getOnline(){
  4.       return online;
  5.     }
  6.     public static void raise(){ 
  7.        online++;
  8.     }
  9.     public static void reduce(){ 
  10.        online--; 
  11.    }
 
 
 
  1. import javax.servlet.http.HttpSessionEvent;
  2. import javax.servlet.http.HttpSessionListener;
  3. public class OnlineCounterListener implements HttpSessionListener{
  4.     public void sessionCreated(HttpSessionEvent hse) { 
  5.         OnlineCounter.raise();  
  6.     } 
  7.    public void sessionDestroyed(HttpSessionEvent hse){  
  8.         OnlineCounter.reduce();
  9.     } 
  10.  } 

在需要顯示在線人數(shù)的JSP中可是使用目前在線人數(shù):

 
 
 
  1. ﹤%@ page import=“xxx.OnlineCounter" %﹥
  2. ﹤%=OnlineCounter.getOnline()%﹥

退出會話(可以給用戶提供一個注銷按鈕):

 
 
 
  1. ﹤form action="exit.jsp" method=post﹥ 
  2.   ﹤input type=submit value="exit"﹥ 
  3. ﹤/form﹥ 

exit.jsp: ﹤%session.invalidate() ;%﹥

在web.xml中加入:

 
 
 
  1. ﹤listener﹥ 
  2.   ﹤listener-class﹥servletlistener111111.SecondListener﹤/listener-class﹥
  3. ﹤/listener﹥

Servlet監(jiān)聽器怎么樣,就是這么簡單,不用對現(xiàn)有代碼做任何的修改。趕緊試試吧!


當前題目:Servlet監(jiān)聽器概念特點常用概述
URL鏈接:http://m.5511xx.com/article/cdodeeg.html