更新時間:2020-12-18 15:56:53 來源:動力節(jié)點 瀏覽2856次
監(jiān)聽器也叫Listener,是Servlet的監(jiān)聽器,它可以監(jiān)聽客戶端的請求、服務(wù)端的操作等。通過監(jiān)聽器,可以自動激發(fā)一些操作,比如監(jiān)聽在線的用戶的數(shù)量。
一. 監(jiān)聽器
1. 簡介
Listener的作用是用于監(jiān)聽web應(yīng)用的創(chuàng)建和銷毀,以及在其上attribute發(fā)生的變化。
web應(yīng)用即ServletContext對象(jsp的隱式對象application)
除了對web應(yīng)用的監(jiān)聽外,還能監(jiān)聽session和request的生命周期,以及他們的attribute發(fā)生的變化。
Javaweb開發(fā)中的監(jiān)聽器是用于監(jiān)聽web常見對象HttpServletRequest,HttpSession,ServletContext。
2. 監(jiān)聽器的作用
監(jiān)聽web對象創(chuàng)建與銷毀.
監(jiān)聽web對象的屬性變化
3. 創(chuàng)建一個監(jiān)聽器的步驟
創(chuàng)建一個類,實現(xiàn)需要監(jiān)聽器的接口
重寫接口中的方法
在web.xml中配置注冊該監(jiān)聽器
二. Listender監(jiān)聽Context
1.監(jiān)聽ServletContext的生命周期
//需要實現(xiàn)ServletContextListener
public?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
對于Java WEB開發(fā),我們一直在使用的是servlet和jsp的形式。除了這兩種開發(fā)必備組件之外,還有一些在實際項目開發(fā)中必須要用到的輔助技術(shù)。比如Filter和Listener。
動力節(jié)點Listener監(jiān)聽器視頻教程,本課程會針對Filter技術(shù),從使用原理到實際應(yīng)用,做一個全面的講解。最后還會搭配一個完整的攔截請求的綜合案例。對于Listener,在未來的實際項目開發(fā)中,應(yīng)用的并不是很廣泛,但是Listener的原理是非常重要的,所以本課程會對針Listener的原理進行重點講解。

以上就是動力節(jié)點Java培訓機構(gòu)的小編針對“Java listener視頻,監(jiān)聽器的使用”的內(nèi)容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為你服務(wù)。