Java Web笔记 - Servlet中的Listener监听器的介绍 常用监听器接口 实现监听器

发布于 2011-11-12 | 更新于 2020-09-20

Listener是Servlet的监听器,它可以监听客户端的请求,服务器端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户数量。

比如,当增加一个HttpSession时,就自动触发sessionCreated(HttpSessionEvent se)方法,在这个方法中就可以统计在线人数了。

使用到了观察者模式。

1、常用的监听接口:

1.1、ServletContextAttributeListener

监听ServletContext属性的操作,比如增加,删除或修改属性。

其中每个方法都提供了一个监听事件类:ServletContextAttributeEvent可以从这个类中获取被监听到的属性的名称和值。

attributeAdded
void attributeAdded(ServletContextAttributeEvent scab)

Notification that a new attribute was added to the servlet context. Called after the attribute is added.

attributeRemoved
void attributeRemoved(ServletContextAttributeEvent scab)

Notification that an existing attribute has been removed from the servlet context. Called after the attribute is removed.

attributeReplaced
void attributeReplaced(ServletContextAttributeEvent scab)

Notification that an attribute on the servlet context has been replaced. Called after the attribute is replaced.

1.2、ServletContextListener

当创建ServletContext对象时,激发contextInitialized方法;当销毁ServletContext对象时,激发contextDestroyed方法。

1.3、HttpSessionListener

当创建一个Session对象时,激发sessionCreated事件;当销毁一个Session对象时,激发sessionDestroyed事件。

sessionCreated
void sessionCreated(HttpSessionEvent se)

Notification that a session was created.
Parameters:
se - the notification event

sessionDestroyed
void sessionDestroyed(HttpSessionEvent se)

Notification that a session is about to be invalidated.
Parameters:
se - the notification event

HttpSessionEvent事件类:

public class HttpSessionEvent
extends EventObject

This is the class representing event notifications for changes to sessions within a web application.

相关方法:

getSession
public HttpSession getSession()

Return the session that changed.

1.4、HttpSessionAttributeListener

监听HttpSession中的属性操作,在HttpSession中添加一个属性时,激发attributeAdded方法;当在Session中删除一个属性时,激发attributeRemoved方法;当Session属性被重新设置时,激发attributeReplaced方法。

2、一个监听器的实现步骤:

这里编写一个ServletContextAttributeListener的实现类:

public class MyServletContextAttributeListener implements
ServletContextAttributeListener {

@Override
public void attributeAdded(ServletContextAttributeEvent scab) {
    System.out.println("添加属性:" \+ scab.getName() + scab.getValue());
}

@Override
public void attributeRemoved(ServletContextAttributeEvent scab) {
    System.out.println("删除属性:" \+ scab.getName() + scab.getValue());
}

@Override
public void attributeReplaced(ServletContextAttributeEvent scab) {
    System.out.println("修改属性" \+ scab.getName() + scab.getValue());
}

}

在web.xml部署描述符中部署:

com.itzhai.listener.MyServletContextAttributeListener

注意:监听器一般配置在拦截器的前面。

这样当有JSP或者Servlet添加,删除或修改了ContextAttribute的属性时就会触发相应的事件:

<%
//触发添加事件
application.setAttribute(“username”, “arthinking”);
//触发修改事件
application.setAttribute(“username”, “Jason”);
%>

在开发中一般都是由框架实现这些接口。

本文作者: arthinking

本文链接: https://www.itzhai.comjava-web-notes-servlet-listener-listener-in-the-listener-interface-commonly-used-in-listener.html

版权声明: 版权归作者所有,未经许可不得转载,侵权必究!联系作者请加公众号。

×
IT宅

关注公众号及时获取网站内容更新。