监听器(Listener)是一种特殊的Servlet技术, 它可以监听Web应用的上下文信息、Servlet请求信息和Servlet会话信息, 即ServletContext、ServletRequest、HTTPSession。并根据不同情况, 在后台调用响应的处理程序。 利用监听器对Web应用进行监听和控制,来增强Web应用的事件处理能力。
按监听对象划分:
ServletContext对象监听器ServletRequest对象监听器HTTPSession对象监听器按监听事件划分:
对象自身的创建和销毁的监听器对象中属性的增加、消除和替换的监听器session中的某个对象的状态变化的监听器可以监听到ServletContext对象中属性的创建、删除和修改操作。常用监听方法:
接口名称接口方法激发条件ServletContextAttributeListenerpublic void attributeAdded(ServletContextAttributeEvent scab);增加属性public void attributeRemoved(ServletContextAttributeEvent scab);删除属性public void attributeRepalced(ServletContextAttributeEvent scab);修改属性ServletContextListenerpublic void contextInitialized(ServletContextEvent sce);创建对象public void contextDestroyed(ServletContextEvent sce);销毁对象可以监听到HTTPSession对象中属性的创建、删除和修改操作.也可以监听HTTPSession对象本身的创建和销毁,以及
接口名称接口方法激发条件HttpSessionAttributeListenerpublic attributeAdded(HttpSessionBindingEvent se);增加属性public void attributeRemoved(HttpSessionBindingEvent se);删除属性public void attributeReplaced(HttpSessionBindingEvent se);修改属性HttpSessionListenerpublic void sessionCreated(HttpSessionEvent se);创建对象public void sessionDestroyed(HttpSessionEvent se);销毁对象HttpSessionActivationListenerpublic void sessionDidActivate(HttpSessionEvent se);会话刚被激活public void sessionWillPssivate(HttpSessionEvent se)会话将要钝化HttpSessionBindingListenerpublic void valueBound(HttpSessionBindingEvent se);调用setAttribute()public void valueUnbound(HttpSessionBindingEvent se);调用removeAttribute() 注意:活化(Activate)和钝化(Passivate)是Web容器为了更好地利用系统资源或者进行服务器负载平衡等原因而对特定对象采取的措施。 会话对象的钝化是指暂时将会话对象通过序列化的方法存储到硬盘上; 活化与钝化相反,是把硬盘上存储的会话对象重新加载到Web容器中。 session的销毁有两种情况 (1)session超时,web.xml配置:
<session-config> <session-timeout>120</session-timeout><!--session120分钟后超时销毁--> </session-config>(2)手动方式:
public void invalidate();//使session失效方法。session.invalidate();能够监听到ServletRequest对象中属性的增加、删除和修改操作, 也可以监听到ServletRequest对象本身的增加和删除, 常用方法:
接口名称接口方法激发条件ServletRequestAttributeListenerpublic void attributeAdded(ServletRequestAttributeEvent srae);增加属性public void attributeRemoved(ServletRequestAttributeEvent srae);删除属性public void attributeReplaced(ServletRequestAttributeEvent srae);修改属性ServletRequestListenerpublic void requestInitialized(ServletRequestEvent sre);创建对象public void requestDestroyed(ServletRequestEvent sre);销毁对象1)当有多个监听器时,按各监听器在web.xml中配置的顺序进行先后顺序启动。 2)相较于过滤器、Servlet的启动顺序是:监听器>过滤器>Servlet
实现对应接口的监听器类:
package com.xzk.web; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * @Author: lqk * @Description: * @Date Created in 2020-07-04 9:03 * @Modified By: */ public class ListenerServlet implements HttpSessionListener { //存储当前在线人数 private static int count = 0; @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { count++; } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { if(count>0) { count--; } } public static int getCount(){ return count; } }web.xml 配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <listener> <listener-class>com.xzk.web.ListenerServlet</listener-class> </listener> </web-app>jsp页面:
<%@ page import="com.xzk.web.ListenerServlet" %><%-- Created by IntelliJ IDEA. User: lqk Date: 2020/7/4 Time: 9:01 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="com.xzk.web.*" %> <html> <head> <title>$Title$</title> </head> <body> 在线人数:<%=ListenerServlet.getCount()%> </body> </html>使用IDEA自动启动浏览器 显示在线人数是3 ?? 解决 关掉自动启动