Java listener視頻,監(jiān)聽器的使用

Java listener視頻,監(jiān)聽器的使用

長(zhǎng)沙達(dá)內(nèi)教育      2022-04-14 09:21:01     6

Java listener視頻,監(jiān)聽器的使用,監(jiān)聽器也叫Listener,是Servlet的監(jiān)聽器,它可以監(jiān)聽客戶端的請(qǐng)求、服務(wù)端的操作等。通過監(jiān)聽器,可以自動(dòng)激發(fā)一些操作,比如監(jiān)

課程價(jià)格 請(qǐng)咨詢

上課時(shí)段: 授課校區(qū):

詳細(xì)介紹

監(jiān)聽器也叫Listener,是Servlet的監(jiān)聽器,它可以監(jiān)聽客戶端的請(qǐng)求、服務(wù)端的操作等。通過監(jiān)聽器,可以自動(dòng)激發(fā)一些操作,比如監(jiān)聽在線的用戶的數(shù)量。

一. 監(jiān)聽器

1. 簡(jiǎn)介

Listener的作用是用于監(jiān)聽web應(yīng)用的創(chuàng)建和銷毀,以及在其上attribute發(fā)生的變化。

web應(yīng)用即ServletContext對(duì)象(jsp的隱式對(duì)象application)

除了對(duì)web應(yīng)用的監(jiān)聽外,還能監(jiān)聽session和request的生命周期,以及他們的attribute發(fā)生的變化。

Javaweb開發(fā)中的監(jiān)聽器是用于監(jiān)聽web常見對(duì)象HttpServletRequest,HttpSession,ServletContext。

2. 監(jiān)聽器的作用

監(jiān)聽web對(duì)象創(chuàng)建與銷毀.

監(jiān)聽web對(duì)象的屬性變化

3. 創(chuàng)建一個(gè)監(jiān)聽器的步驟

創(chuàng)建一個(gè)類,實(shí)現(xiàn)需要監(jiān)聽器的接口

重寫接口中的方法

在web.xml中配置注冊(cè)該監(jiān)聽器

二. Listender監(jiān)聽Context

1.監(jiān)聽ServletContext的生命周期

//需要實(shí)現(xiàn)ServletContextListenerpublic?class?ContextListener?implements?ServletContextListener?{????@Override????public?void?contextInitialized(ServletContextEvent?sce)?{?????????System.out.println("web應(yīng)用初始化");????}?????@Override????public?void?contextDestroyed(ServletContextEvent?sce)?{?????????System.out.println("web應(yīng)用銷毀");????}?}????com.joe.listener.ContextListener

2. 監(jiān)聽 ServletContext 上屬性的變化

public?class?ContextAttributeListener?implements?ServletContextAttributeListener?{????@Override????public?void?attributeAdded(ServletContextAttributeEvent?e)?{?????????System.out.println("被添加的屬性,名:"+e.getName()+"?值:"+e.getValue());????}?????@Override????public?void?attributeRemoved(ServletContextAttributeEvent?e)?{?????????System.out.println("屬性被移出,名:"+e.getName()+"?值:"+e.getValue());????}?????@Override????public?void?attributeReplaced(ServletContextAttributeEvent?e)?{????????//獲取到的是修改前的值?????????System.out.println("屬性被修改,名:"+e.getName()+"?值:"+e.getValue());????}}javascript:void(0)com.joe.listener.ContextAttributeListener<%????application.setAttribute("name","joe");????application.setAttribute("name","joe1");????application.removeAttribute("name");???%>

三. Listener監(jiān)聽Session

1.監(jiān)聽HttpSession的創(chuàng)建與銷毀以及其上屬性的變化

public?class?SessionListener?implements?HttpSessionListener,?HttpSessionAttributeListener?{????@Override????public?void?sessionCreated(HttpSessionEvent?se)?{?????????System.out.println("session?被創(chuàng)建");????}?????@Override????public?void?sessionDestroyed(HttpSessionEvent?se)?{?????????System.out.println("session?被銷毀");????}?????@Override????public?void?attributeAdded(HttpSessionBindingEvent?e)?{????????System.out.println("被添加的屬性,名:"+e.getName()+"?值:"+e.getValue());????}?????@Override????public?void?attributeRemoved(HttpSessionBindingEvent?e)?{????????System.out.println("屬性被移出,名:"+e.getName()+"?值:"+e.getValue());????}?????@Override????public?void?attributeReplaced(HttpSessionBindingEvent?e)?{????????//獲取到的是修改前的值????????System.out.println("屬性被修改,名:"+e.getName()+"?值:"+e.getValue());????}??}??????com.joe.listener.SessionListener

對(duì)于Java WEB開發(fā),我們一直在使用的是servlet和jsp的形式。除了這兩種開發(fā)必備組件之外,還有一些在實(shí)際項(xiàng)目開發(fā)中必須要用到的輔助技術(shù)。比如Filter和Listener。

長(zhǎng)沙達(dá)內(nèi)教育Listener監(jiān)聽器視頻教程,本課程會(huì)針對(duì)Filter技術(shù),從使用原理到實(shí)際應(yīng)用,做一個(gè)全面的講解。最后還會(huì)搭配一個(gè)完整的攔截請(qǐng)求的綜合案例。對(duì)于Listener,在未來的實(shí)際項(xiàng)目開發(fā)中,應(yīng)用的并不是很廣泛,但是Listener的原理是非常重要的,所以本課程會(huì)對(duì)針Listener的原理進(jìn)行重點(diǎn)講解。

以上就是長(zhǎng)沙達(dá)內(nèi)教育Java培訓(xùn)機(jī)構(gòu)的小編針對(duì)“Java listener視頻,監(jiān)聽器的使用”的內(nèi)容進(jìn)行的回答,希望對(duì)大家有所幫助,如有疑問,請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。

培訓(xùn)啦提醒您:交易時(shí)請(qǐng)核實(shí)對(duì)方資質(zhì),對(duì)于過大宣傳或承諾需謹(jǐn)慎!任何要求預(yù)付定金、匯款等方式均存在風(fēng)險(xiǎn),謹(jǐn)防上當(dāng)。