Listener--------监听器
Listener:監聽器的原理和實例
八大監聽器:
????? ServletRequest對象:
???????? 生命周期監聽:ServletRequestListener
???????? 屬性監聽:ServletRequestAttributeListener
????? HttpSession對象:
???????? 生命周期監聽:HttpSessionListener
???????? 屬性監聽:HttpSessionAttributeListener
???????? 對象綁定監聽1:HttpSessionBindingListener
???????? 對象鈍化活化監聽2:HttpSessionActivationListener
????? ServletContext對象:
???????? 生命周期監聽:ServletContextListener
???????? 屬性監聽:ServletContextAttributeListener
一、監聽對象,主要監聽servlet的三大域對象request,session,application(ServletRequest,HttpSession,ServletContext)
??? 監聽內容:
??????? A:監聽域對象的創建和銷毀,也就是生命周期的監聽
??????? B:監聽域對象的屬性添加,移除和更改
??????? C:監聽被加入域對象中的對象
二、三大域對象的生命周期
??? ServletRequest 什么時候被創建:
??????? A: 請求一個jsp頁面時,tomcat 翻譯jsp頁面為一個servlet類,執行servlet 類中的service()方法是,tomcat幫我們創建了9大內置對象,其中就有request。
??????? B: 請求一個servlet時,doGet() 或者是doPost()方法,tomcat 自動也會幫我們創建request,
?? ServletRequest 什么時候被銷毀:
??????? 執行完service()方法或者 doGet()方法,doPost()方法即銷毀
??? HttSesssion 什么時候被創建:
??????? Request.getSession():? 先判斷是否存在session對象,如果存在就直接返回,如果不存在就幫我們創建一個session,然后返回。
??? HttSesssion 什么時候被銷毀:
?????? 是根據cookie的生命周期來判斷的,如果cookie是瀏覽器生命周期的話,那么瀏覽器關閉后,session即銷毀;如果cookie是保存到文件中的,那么就具體情況,具體對待。
?
??? ServletContext 什么時候被創建:
?????? Tomcat啟動項目,即創建了application對象。
??? ServletContext 什么時候被銷毀:
?????? Tomcat關閉項目,即銷毀了application對象。
三、【實例】
?? 創建一個監聽器
? A:選擇一個接口,根據監聽對象,監聽內容的不同選擇不同的接口
? B:在web.xml中添加監聽器的配置信息
????? <listener>
????????? <listener-class>監聽器文件的相對路徑</listener-class>
????? </listener>
監聽器: HttpSessionAttributeListener
??? 【原理】
??? HttpSessionAttributeListener是對SessionAttribute的監聽,當在會話對象中加入屬性,移除屬性和替換屬性時就會觸發HttpSessionAttributeListener監
聽器。
??? 接口HttpSessionAttributeListener有3個方法:
??? 1、public void attributeAdded(HttpSessionBindingEvent sbe){}:該方法在session添加對象是觸發
??? 2、public void attributeRemoved(HttpSessionBindingEvent sbe){}:該方法是在session移除對象時觸發的
??? 3、public voidattributeReplaced(HttpSessionBindingEvent se):在Session屬性被重新設置時
?? 【實例】統計一下系統的登錄人數
package com.aaa.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;
@WebListener()
public class Listener2 implements ServletContextListener,
??????? HttpSessionListener, HttpSessionAttributeListener {
????? public int count=0;//統計登錄的人數
??? // Public constructor is required by servlet spec
??? public Listener2() {
??? }
??? // -------------------------------------------------------
??? // ServletContextListener implementation
??? // -------------------------------------------------------
??? public void contextInitialized(ServletContextEvent sce) {
????? /* This method is called when the servlet context is
???????? initialized(when the Web application is deployed).
???????? You can initialize servlet context related data here.
????? */
??? }
??? public void contextDestroyed(ServletContextEvent sce) {
????? /* This method is invoked when the Servlet Context
???????? (the Web application) is undeployed or
???????? Application Server shuts down.
????? */
??? }
??? // -------------------------------------------------------
??? // HttpSessionListener implementation
??? // -------------------------------------------------------
??? public void sessionCreated(HttpSessionEvent se) {
??????? /* Session is created. */
??? }
??? public void sessionDestroyed(HttpSessionEvent se) {
??????? /* Session is destroyed. */
??? }
??? // -------------------------------------------------------
??? // HttpSessionAttributeListener implementation
??? // -------------------------------------------------------
??? public void attributeAdded(HttpSessionBindingEvent sbe) {
????? /* This method is called when an attribute
???????? is added to a session.
????? */
???? //session對象中加入屬性了觸發此方法
???? //sbe.getName()得到session里的屬性名
????? if(sbe.getName().equals("user")){
???????? count++;
???????? //將count放在全局域對象ServletContext里
???????? sbe.getSession().getServletContext().setAttribute("count",count);
???????? System.out.println("現在系統登錄的用戶人數"+count);
?????? }
????? ?
??? }
??? public void attributeRemoved(HttpSessionBindingEvent sbe) {
????? /* This method is called when an attribute
???????? is removed from a session.
????? */
????? //session對象中刪除屬性時觸發此方法
????? if(sbe.getName().equals("user")){
?????????? count--;
?????????? sbe.getSession().getServletContext().setAttribute("count",count);
?????????? System.out.println("現在系統登錄的用戶人數"+count);
?????? }
??? }
??? public void attributeReplaced(HttpSessionBindingEvent sbe) {
????? /* This method is invoked when an attibute
???????? is replaced in a session.
????? */
??? }
}
轉載于:https://www.cnblogs.com/fbbg/p/11099169.html
總結
以上是生活随笔為你收集整理的Listener--------监听器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P1262_美帝的间谍网络被我部捕获!
- 下一篇: 清明出游,在高速上堵了16个小时。