1. 监听器概述
servlet和filter及listener 并称servlet三大技术。
a. listener主要功能是实现对某一个事件的监听,一旦触发了监听事件,则监听器就会做出对应的操作。
2. 监听器的种类
一共可以分为三类,八种监听器。
a. 监听域的创建和销毁的监听器。
request session servletContext |
ServletRequestListener HttpSessionListener ServletContextListener |
为什么没有监听pageContext域的,因为这个就是页面,我们可以直接查看,无需监听。
代码实现:
1.监听request域
package cn.tedu.listener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; //监听request域创建于销毁的监听器 public class RequestListenerDemo1 implements ServletRequestListener { //请求销毁时触发 public void requestDestroyed(ServletRequestEvent sre) { System.out.println("请求结束.."+sre.getServletRequest()); } //请求创建时触发 public void requestInitialized(ServletRequestEvent sre) { System.out.println("请求开始.."+sre.getServletRequest()); } }
web配置文件
<!-- request域监听器 --> <listener> <listener-class>cn.tedu.listener.RequestListenerDemo1</listener-class> </listener>
2.监听session域
package cn.tedu.listener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; //监听session域的创建于销毁的监听器 public class SessionListenerDemo1 implements HttpSessionListener { //监听session域的创建 public void sessionCreated(HttpSessionEvent se) { System.out.println("session域创建..."+se.getSession()); } //监听session域的销毁 public void sessionDestroyed(HttpSessionEvent se) { System.out.println("session域销毁..."+se.getSession()); } }
配置文件
<!-- session域监听器 --> <listener> <listener-class>cn.tedu.listener.SessionListenerDemo1</listener-class> </listener>
jsp页面有个配置指令 默认session是true也就是当访问该jsp页面的时候会自动创建一个session对象
3.监听servletContext域
package cn.tedu.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; //监听servletContext域的创建与销毁 public class ServletContextListenerDemo1 implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { System.out.println("day15开始.."+sce.getServletContext()); } public void contextDestroyed(ServletContextEvent sce) { System.out.println("day15结束.."+sce.getServletContext()); } }
配置文件
<!-- ServletContext域监听器 --> <listener> <listener-class>cn.tedu.listener.ServletContextListenerDemo1</listener-class> </listener>
在当前这一类监听器中,需要先创建一个监听器的类,在添加监听器相关配置。
监听域中属性创建修改删除的监听器
代码实现
package cn.tedu.listener; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; //servletcontext域属性监听器 public class SCListenerDemo1 implements ServletContextAttributeListener { //在属性创建时触发 public void attributeAdded(ServletContextAttributeEvent scab) { System.out.println("ServletContext属性:"+scab.getName()+"创建"+scab.getValue()); } //在属性删除时触发 public void attributeRemoved(ServletContextAttributeEvent scab) { System.out.println("ServletContext属性:"+scab.getName()+"删除"+scab.getValue()); } //在属性修改时触发 public void attributeReplaced(ServletContextAttributeEvent scab) { System.out.println("ServletContext属性:" +scab.getName()+"替换"+scab.getValue()+"替换后"+scab.getServletContext().getAttribute("name")); //替换后获取的是当前域中对象 } }
添加web.xml信息
<!-- ServletContext域属性监听器 --> <listener> <listener-class>cn.tedu.listener.SCListenerDemo1</listener-class> </listener>
c. 监听javabean在session域中变化的监听器
HttpSessionBindingListener
HttpSessionActivationListener
此类监听器在使用时,直接在javabean身上实现对应的接口,不需要添加相关配置信息。
HttpSessionActivationListener 让javabean自己感知到自己随着session被钝化和活化
i. HttpSessionBindingListener
让javabean自己感知到自己被存入或移除出session的状态变化的监听器
此监听器不需要单独开发类,也不需要在web.xml中进行配置,而是直接让需要监听的JavaBean实现即可。
package cn.tedu.domain; import java.io.Serializable; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; public class Person implements HttpSessionBindingListener{ private String name; private int age; private String gender; public Person(){ } public Person(String name, int age, String gender) { super(); this.name = name; this.age = age; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } //javabean进入session域时触发 public void valueBound(HttpSessionBindingEvent event) { System.out.println("javabean进入session"+name); } //javabean移除session域时触发 public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("javabean离开session"+name); } }
项目中的使用
package com.easymall.listener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpServletRequest; public class RequestListener implements ServletRequestListener{ public void requestDestroyed(ServletRequestEvent sre) { // 1.获取请求对象 HttpServletRequest request = (HttpServletRequest) sre.getServletRequest(); //2.获取请求对象的参数。ip。url String url = request.getRequestURL().toString(); String ip = request.getRemoteAddr(); String username = request.getParameter("username"); if(username == null){ username = "游客"; } System.out.println("用户【"+username+"】访问地址【"+url+"】客户机ip【"+ip+"】请求结束"); } public void requestInitialized(ServletRequestEvent sre) { // 1.获取请求对象 HttpServletRequest request = (HttpServletRequest) sre.getServletRequest(); //2.获取请求对象的参数。ip。url String url = request.getRequestURL().toString(); String ip = request.getRemoteAddr(); String username = request.getParameter("username"); if(username == null){ username = "游客"; } System.out.println("用户【"+username+"】访问地址【"+url+"】客户机ip【"+ip+"】请求开始"); } }
package com.easymall.listener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpServletRequest; public class RequestListener implements ServletRequestListener{ public void requestDestroyed(ServletRequestEvent sre) { // 1.获取请求对象 HttpServletRequest request = (HttpServletRequest) sre.getServletRequest(); //2.获取请求对象的参数。ip。url String url = request.getRequestURL().toString(); String ip = request.getRemoteAddr(); String username = request.getParameter("username"); if(username == null){ username = "游客"; } System.out.println("用户【"+username+"】访问地址【"+url+"】客户机ip【"+ip+"】请求结束"); } public void requestInitialized(ServletRequestEvent sre) { // 1.获取请求对象 HttpServletRequest request = (HttpServletRequest) sre.getServletRequest(); //2.获取请求对象的参数。ip。url String url = request.getRequestURL().toString(); String ip = request.getRemoteAddr(); String username = request.getParameter("username"); if(username == null){ username = "游客"; } System.out.println("用户【"+username+"】访问地址【"+url+"】客户机ip【"+ip+"】请求开始"); } }
<!--SerlvetContextlistener --> -<listener> <listener-class>com.easymall.listener.SCListener</listener-class> </listener> -<listener> <listener-class>com.easymall.listener.RequestListener</listener-class> </listener>