监听器Listener
监听器Listener
监听器Listener定义与作用
监听器Listener用通俗的语言说就是在application,session,request三个对象创建消亡或者往其中添加修改删除属性时自动执行代码的功能组件。
比如我们想在application对象创建或消亡时执行某些代码,只要实现ServletContextListener接口即可,当创建系统调用application对象时,便会调用与之对应的方法
比如我们想在调用application对象的setAttribute(),getAttribute(),removeAttribute()方法时执行某些代码也只要实现ServletContextAttributeListener接口即可,当调用application对象的attribute方法,便会调用实现类对应方法
监听器Listener分类
ServletContextAttributeListener监听对application属性的操作,比如setAttribute(),getAttribute(),removeAttribute()增加、删除、修改属性调用对应方法。ServletContextListener监听ServletContext。当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed(HttpSessionEvent se)方法。HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se)方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se)方法。
与request相关的监听器有ServletRequestAttributeListener与ServletRequestListener
监听器Listener实现案例
package com.wangwang; import javax.servlet.ServletContext; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; /** * 西安云工厂旺旺老师Listener案例 * @author www.xaygc.com * */ public class AppAttListener implements ServletContextAttributeListener { public void attributeAdded(ServletContextAttributeEvent event) { ServletContext application = event.getServletContext(); application.log("application中添加属性,名:" + event.getName() + " 值:" + event.getValue()); System.out.println("application中添加属性,名:" + event.getName() + " 值:" + event.getValue()); } public void attributeRemoved(ServletContextAttributeEvent event) { ServletContext application = event.getServletContext(); application.log("application中删除属性,名:" + event.getName() + " 值:" + event.getValue()); System.out.println("application中删除属性,名:" + event.getName() + " 值:" + event.getValue()); } public void attributeReplaced(ServletContextAttributeEvent event) { ServletContext application = event.getServletContext(); application.log("application中修改属性,名:" + event.getName() + " 值:" + event.getValue()); System.out.println("application中修改属性,名:" + event.getName() + " 值:" + event.getValue()); } }
package com.wangwang; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /** * 西安云工厂旺旺老师Listener案例 * @author www.xaygc.com * */ public class ContextListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent event) { System.out.println("----------------------application对象消亡----------------------"); } public void contextInitialized(ServletContextEvent event) { System.out.println("----------------------application对象创建----------------------"); ServletContext application = event.getServletContext(); System.out.println(application.getInitParameter("userName")); System.out.println(application.getInitParameter("springConfigLocation")); //application.getInitParameterNames(); } }
当然还需要去web.xml中配置
<!-- 这是SSH整合的一种典型应用,大家暂时了解,后期讲SSH整合时再反过来看看这个例子 -->
<context-param>
<param-name>springConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>userName</param-name>
<param-value>wangwang</param-value>
</context-param>
<listener>
<listener-class>com.wangwang.ContextListener</listener-class>
</listener>
<listener>
<listener-class>com.wangwang.AppAttListener</listener-class>
</listener>
一个统计最多在线用户人数的监听器案例
package com.wangwang; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletContext; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * 西安云工厂旺旺老师Listener案例 * @author www.xaygc.com */ public class HttpSessionListenerImpl implements HttpSessionListener { public void sessionCreated(HttpSessionEvent event) { ServletContext app = event.getSession().getServletContext(); int count = Integer.parseInt(app.getAttribute("onLineCount").toString()); count++; app.setAttribute("onLineCount", count); int maxOnLineCount = Integer.parseInt(app.getAttribute("maxOnLineCount").toString()); if (count > maxOnLineCount) { //记录最多人数是多少 app.setAttribute("maxOnLineCount", count); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //记录在那个时刻达到上限 app.setAttribute("date", df.format(new Date())); } } //停止tomcat不会调用 //注销 //超过过期时间 public void sessionDestroyed(HttpSessionEvent event) { ServletContext app = event.getSession().getServletContext(); int count = Integer.parseInt(app.getAttribute("onLineCount").toString()); count--; app.setAttribute("onLineCount", count); } }