Listener详解

Listener

基本介绍

什么是监听器

Javaweb中的监听器是用于监听web常见对象HttpServletRequest, HttpSession, ServletContext

使用方法

作用

在JavaWeb中Listener是Servlet规范定义的一种特殊类,主要用于监听3个作用域的创建、销毁,以及其属性变更

Servlet中的三个作用域分别为

HttpServletRequest

HttpSession

ServletContext

HttpServletRequest作用域对应的监听器是ServletRequestListener(监听request的创建和销毁ServletRequestAttributeListener(监听request中属性的变更)

HttpSession作用域对应的监听器是HttpSessionListener(监听session的创建)和HttpSessionAttributeListener(监听session中属性的变更)

ServletContext作用域对应的监听器是ServletContextListener(监听servletContext的创建于销毁)和ServletContextAttributeListener(servletContext中属性的变更)

监听机制相关概念

1.事件----一件事情

2.事件源---产生这件事情的源头

3.注册监听---将监听器与事件绑定,当事件产生时,监听器可以知道,并进行处理。

4.监听器---对某件事情进行处理监听的一个对象

监听器的创建步骤

1创建监听器接口

2重写接口方法.

3配置web.xml / 注解

4测试监听器

使用

@Override

public void attributeAdded(ServletContextAttributeEvent scae) {

/*添加属性时调用*/

System.out.println("新增的属性:"+scae.getName()+":"+scae.getValue());

}

@Override

public void attributeRemoved(ServletContextAttributeEvent scae) {

/*属性移除时调用*/

System.out.println("移除的属性:"+scae.getName()+":"+scae.getValue());

}

@Override

public void attributeReplaced(ServletContextAttributeEvent scae) {

/*属性替换时调用(修改值)*/

System.out.println("替换的属性:"+scae.getName()+":"+scae.getValue());

}

@Override

public void contextInitialized(ServletContextEvent sce) {

/*servletContext创建时调用*/

System.out.println("项目启动了");

}

@Override

public void contextDestroyed(ServletContextEvent sce) {

/*servletContext销毁时调用*/

System.out.println("项目停止了");

}

/**

* @Function :

* date 2021/5/22 - 23:28

* How :

*/

@WebListener

public class ListenerDemo implements ServletContextListener,

HttpSessionListener, HttpSessionAttributeListener {

// Public constructor is required by servlet spec

public ListenerDemo() {

}

// -------------------------------------------------------

// ServletContextListener implementation

// ServletContextListener实现

// -------------------------------------------------------

/**

* 服务器启动后自动执行

* @param sce

*/

@Override

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.

当servlet上下文为已初始化(当部署Web应用程序时)。

您可以在这里初始化servlet上下文相关数据。

*/

}

/**

* 服务器正常关闭后被执行

* @param sce

*/

@Override

public void contextDestroyed(ServletContextEvent sce) {

/* This method is invoked when the Servlet Context

(the Web application) is undeployed or

Application Server shuts down.

当Servlet上下文,(Web应用程序)未部署或

应用程序服务器关闭。

*/

}

// -------------------------------------------------------

// HttpSessionListener implementation

// HttpSessionListener实现

// -------------------------------------------------------

public void sessionCreated(HttpSessionEvent se) {

/* Session is created.

session创建 */

}

public void sessionDestroyed(HttpSessionEvent se) {

/* Session is destroyed.

session 销毁 */

}

// -------------------------------------------------------

// HttpSessionAttributeListener implementation

// HttpSessionAttributeListener 实现

// -------------------------------------------------------

public void attributeAdded(HttpSessionBindingEvent sbe) {

/* This method is called when an attribute

is added to a session.

当属性已添加到会话。

*/

}

public void attributeRemoved(HttpSessionBindingEvent sbe) {

/* This method is called when an attribute

is removed from a session.

当属性从会话中删除。

*/

}

public void attributeReplaced(HttpSessionBindingEvent sbe) {

/* This method is invoked when an attribute

is replaced in a session.

当属性在会话中被替换。

*/

}

}