【Servlet】Listener监听器
生活随笔
收集整理的這篇文章主要介紹了
【Servlet】Listener监听器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
監聽器的使用
* 監聽器的使用:* 作用:* 監聽作用域對象request、session、application的創建、銷毀和內容的改變* 使用:* 創建一個實現了指定接口的java類* 監聽request--->ServletRequestListener 監聽request對象的創建和銷毀* requestInitialized(ServletRequestEvent sre)//創建* requestDestroyed(ServletRequestEvent sre)//銷毀* 注意:* 形參可以獲取監聽的request對象* sre.getServletRequest();* 監聽request--->ServletRequestAttributeListener 監聽request作用域數據的變更。attributeAdded(ServletRequestAttributeEvent srae)* attributeRemoved(ServletRequestAttributeEvent srae) * attributeReplaced(ServletRequestAttributeEvent srae)* 注意:形參可以獲取被監聽的數據 * srae.getName() 獲取監聽數據的鍵* srae.getValue() 獲取監聽數據的值* 監聽session--->HttpSessionListener 監聽session的創建和銷毀* sessionCreated(HttpSessionEvent se) 創建* sessionDestroyed(HttpSessionEvent se) 銷毀* 注意:形參可以獲取被監聽的session對象* se.getSession();* 監聽session--->HttpSessionAttributeListener 監聽session數據的變更* attributeAdded(HttpSessionBindingEvent event)* attributeRemoved(HttpSessionBindingEvent event)* attributeReplaced(HttpSessionBindingEvent event)* 注意:形參可以獲取被監聽的數據* event.getName() 獲取數據的鍵名* event.getValue() 獲取數據的值* 監聽application--->ServletContextListener 監聽application對象的初始化和銷毀* contextInitialized(ServletContextEvent sce) 初始化 服務器啟動* contextDestroyed(ServletContextEvent sce) 銷毀 服務器關閉* 注意:* 形參可以獲取當前application對象。* sce.getServletContext();監聽application--->ServletContextAttributeListener 監聽數據的變更attributeAdded(ServletContextAttributeEvent event)* attributeRemoved(ServletContextAttributeEvent event)* attributeReplaced(ServletContextAttributeEvent event)* 注意:* 形參可以獲取當前監聽的數據* event.getName() 獲取數據的鍵名* event.getValue() 獲取數據的值* 在web.xml中配置監聽器類* <listener><listener-class>com.bjsxt.listener.MyListener</listener-class></listener>案例:統計當前在線人數。統計網頁瀏覽器次數。 package com.bjsxt.listener;import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletRequestAttributeEvent; import javax.servlet.ServletRequestAttributeListener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /*** 監聽器的使用*/ public class MyListener implements ServletRequestListener,ServletRequestAttributeListener,HttpSessionListener,HttpSessionAttributeListener,ServletContextListener,ServletContextAttributeListener{//request對象銷毀@Overridepublic void requestDestroyed(ServletRequestEvent sre) {System.out.println("我被銷毀了");}//request對象創建@Overridepublic void requestInitialized(ServletRequestEvent sre) {System.out.println("我被創建了");}//監聽request作用域數據的添加@Overridepublic void attributeAdded(ServletRequestAttributeEvent srae) {System.out.println("request中增加了一條數據-"+srae.getName()+":"+srae.getValue());}@Overridepublic void attributeRemoved(ServletRequestAttributeEvent srae) {// TODO Auto-generated method stub}@Overridepublic void attributeReplaced(ServletRequestAttributeEvent srae) {// TODO Auto-generated method stub} /*------------------------------------------------------------------------------*///監聽session的創建@Overridepublic void sessionCreated(HttpSessionEvent se) {System.out.println("session被創建了");}//監聽session的銷毀@Overridepublic void sessionDestroyed(HttpSessionEvent se) {System.out.println("session被銷毀了");}//監聽session數據的表更@Overridepublic void attributeAdded(HttpSessionBindingEvent event) {System.out.println("session中增加了一條數據"+event.getName()+":"+event.getValue());}@Overridepublic void attributeRemoved(HttpSessionBindingEvent event) {// TODO Auto-generated method stub}@Overridepublic void attributeReplaced(HttpSessionBindingEvent event) {// TODO Auto-generated method stub} /*------------------------------------------------------------------------------*/@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("application對象被初始化了");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {System.out.println("application對象被銷毀了");}//監聽application的數據變更@Overridepublic void attributeAdded(ServletContextAttributeEvent event) {System.out.println("application中存儲了數據:"+event.getName()+":"+event.getValue());}@Overridepublic void attributeRemoved(ServletContextAttributeEvent event) {// TODO Auto-generated method stub}@Overridepublic void attributeReplaced(ServletContextAttributeEvent event) {// TODO Auto-generated method stub} }總結
以上是生活随笔為你收集整理的【Servlet】Listener监听器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Servlet】总结 JSP的四大域对
- 下一篇: 【PAT甲级 最长公共子串】1007 M