监听器
监听器是Servlet规范中定义的一种特殊类
用于监听ServletContext(上下文
)、HttpSession(会话
)和ServletRequest(请求
)等域对象的创建与销毁事件,以及监听这些域对象中属性发生修改的事件。
监听器的分类情况
按监听对象
- 监听应用程序对象(ServletContext)
- 监听用户会话对象(HttpSeesion)
- 监听请求消息对象(ServletRequest)
按监听事件
- 监听对象的创建和销毁的事件监听器。
- 监听对象属性变更(添加,修改,移除)的监听器。
使用的监听器接口
ServletContextListener:上下文监听器。
生命周期: 在服务器启动时创建 ;服务器关闭时销毁.
ServletContextAttributeListener:上下文属性监听器。
ServletRequestListener:请求监听器。
生命周期 :发出请求 时创建;请求结束时销毁.
ServletRequestAttributeListener:请求属性监听器。
HttpSessionListener:会话监听器。
生命周期:在会话开始时创建,当出现服务器关闭
或者客户端不和服务器交互
时;或者服务器强制销毁会话
时; 会话监听器销毁,
HttpSessionAttributeListener:会话属性监听器。
案例
创建一个包放置监听器的实现;
创建 AllLinster
类实现上下文监听接口,会话监听接口,请求监听接口.
public class AllLinster implements ServletContextListener , HttpSessionListener , ServletRequestListener {
//上下文对象监听器的方法;
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("创建上下文监听ServletContext");
//获取servletContext对象;
ServletContext servletContext=sce.getServletContext();
//添加属性值dog,阿汪;
servletContext.setAttribute("dog","阿汪");
//修改属性值dog,阿汪;
servletContext.setAttribute("dog","阿菜");
//添加属性值cat,阿喵;
servletContext.setAttribute("cat","阿喵");
//删除指定属性 dog,阿菜;
servletContext.removeAttribute("dog");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("销毁上下文监听ServletContext");
}
//会话对象监听器的方法;
@Override
public void sessionCreated(HttpSessionEvent se) {
HttpSession session=se.getSession();
System.out.println("创建会话监听HttpSession");
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("销毁会话监听HttpSession");
}
//请求对象监听的方法;
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("创建请求监听ServletRequest");
}
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("销毁请求监听ServletRequest");
}
}
创建Linster_SCAttribute
类;实现上下文属性监听接口;
// ServletContextAttributeListener 上下文属性监听
public class Linster_SCAttribute implements ServletContextAttributeListener {
//添加属性;
@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()+"]");
}
}
在web.xml
文件中配置监听器;
注意一个linstere
标签只能放一个监听类的路径;
<?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">
<!--配置过滤器-->
<filter>
<!--filter-name:过滤器名称-->
<filter-name>aboutEncodingFilter</filter-name>
<!--filter-class:过滤器在当前项目的路径-->
<filter-class>com.xiaozhi.filter.EncodingFilter</filter-class>
</filter>
<!--过滤器的映射-->
<filter-mapping>
<!--需要映射的过滤器名称-->
<filter-name>aboutEncodingFilter</filter-name>
<!--<url-pattern>:设置过滤器的作用地址;匹配对应url地址,有多个地址时可以写多个 -->
<!-- <url-pattern>/login.jsp</url-pattern>-->
<!--或者直接写 /* 匹配当前项目所有的地址-->
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置过滤器-->
<filter>
<!--filter-name:过滤器名称-->
<filter-name>AccessViolationFilter</filter-name>
<!--filter-class:过滤器在当前项目的路径-->
<filter-class>com.xiaozhi.filter.AccessViolationFilter</filter-class>
</filter>
<!--过滤器的映射-->
<filter-mapping>
<!--需要映射的过滤器名称-->
<filter-name>AccessViolationFilter</filter-name>
<!--<url-pattern>:设置过滤器的作用地址;匹配对应url地址-->
<url-pattern>/loginSuccess.jsp</url-pattern>
</filter-mapping>
<!--配置监听器;注意监听类的路径别指向错了-->
<listener>
<listener-class>com.xiaozhi.linster.AllLinster</listener-class>
</listener>
<listener>
<listener-class>com.xiaozhi.linster.Linster_SCAttribute</listener-class>
</listener>
<!--配置默认首选加载页-->
<!--设置默认加载页面-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
login.jsp以及loginSuccess.jsp同之前的案例;点击此处即可跳转
启动服务器后;可看到控制台的输出;上下文监听对象创建了,然后再添加、修改、删除属性时;上下文属性监听器在调用;
访问登录页面;这里添加了三个属性;由于客户端和数据库产生交互,即会话创建;
客户端创建请求,请求结束后销毁;
在停止服务器后;销毁会话监听,以及上下文监听.