Listener监听器

来源:互联网 发布:两个数据库数据同步 编辑:程序博客网 时间:2024/06/07 18:48

需要实现的接口

  ServletContext域 HttpSession域 ServletRequest域 域对象的创建与销毁 ServletContextListener HttpSessionListener ServletRequestListener 域对象内属性的变化 ServletContextAttributeListener HttpSessionAttributeListener ServletRequestAttributeListener

XML中的配置

<listener>    <listener-class>全包名</listener-class></listener>

一、ServletContextListener监听器的主要作用
a、初始化的工作:初始化对象 初始化数据 —- 加载数据库驱动 连接池的初始 化
b、加载一些初始化的配置文件 — spring的配置文件
c、任务调度—-定时器—-Timer/TimerTask

    public void contextDestroyed(ServletContextEvent arg0) {     ServletContext servletContext = arg0.getServletContext();//获得ServletContext对象        //定时调度     Timer t= new Timer();     SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");       Date startDate = null;    try {        startDate = dateFormatter.parse("2017/8/11 00:20:00");    } catch (ParseException e) {        e.printStackTrace();    }      t.scheduleAtFixedRate(new TimerTask() {        public void run() {            System.out.println(); //scheduleAtFixedRate这个方法是        下一次的执行时间点=上一次程序开始执行的时间点+间隔时间        }    }, startDate, 3*60*1000);    t.schedule(new TimerTask() {        public void run() {            System.out.println(); //schedule这个方法是        下一次的执行时间点=上一次程序执行完成的时间点+间隔时间        }    }, startDate, 3*60*1000);    }    @Override    public void contextInitialized(ServletContextEvent arg0) {        // TODO Auto-generated method stub    }



二、监听Httpsession域的创建于销毁的监听器HttpSessionListener
HttpSession对象的生命周期
何时创建:第一次调用request.getSession时创建
何时销毁:服务器关闭销毁 session过期 手动销毁


三、监听三大域对象的属性变化的
setAttribute(name,value)
— 触发添加属性的监听器的方法
— 触发修改属性的监听器的方法
getAttribute(name)
removeAttribute(name)
— 触发删除属性的监听器的方法
1.ServletContextAttibuteListener监听器

public class MyServletContextAttributeListener implements ServletContextAttributeListener{    public void attributeAdded(ServletContextAttributeEvent arg0) {        arg0.getName(); //获取添加的...        arg0.getValue();    }    public void attributeRemoved(ServletContextAttributeEvent arg0) {        arg0.getName(); //获取删除的...        arg0.getValue();    }    public void attributeReplaced(ServletContextAttributeEvent arg0) {        arg0.getName(); //获取修改之前的...        arg0.getValue();    }}

四、与session中的绑定的对象相关的监听器(对象感知监听器)
相对于对象而言
绑定状态:就一个对象被放到session域中
解绑状态:就是这个对象从session域中移除了
钝化状态:是将session内存中的对象持久化(序列化)到磁盘
活化状态:就是将磁盘上的对象再次恢复到session内存中

可能问的面试题:当用户很对时,怎样对服务器进行优化?
用钝化与活化
(1)绑定与解绑的监听器HttpSessionBindingListener
不用在xml中进行配置

public class Person implements HttpSessionBindingListener {    private String name;    private int age;    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 void valueBound(HttpSessionBindingEvent arg0) {        System.out.println("绑定了");        System.out.println(arg0.getName());    }    public void valueUnbound(HttpSessionBindingEvent arg0) {    }}

(2)钝化与活化的监听器HttpSessionActivationListener
也要实现序列化接口

public class Car implements HttpSessionActivationListener,Serializable{    private String type;    private int price;    public String getType() {        return type;    }    public void setType(String type) {        this.type = type;    }    public int getPrice() {        return price;    }    public void setPrice(int price) {        this.price = price;    }    public void sessionDidActivate(HttpSessionEvent arg0) {        //活化    }    public void sessionWillPassivate(HttpSessionEvent arg0) {        //钝化    }}

可以通过配置文件 指定对象钝化时间 — 对象多长时间不用被钝化
在META-INF下创建一个context.xml

<Context> <!-- maxIdleSwap:session中的对象多长时间不使用就钝化 --> <!-- directory:钝化后的对象的文件写到磁盘的哪个目录下  配置钝化的对象文件在                                             work/catalina/localhost/钝化文件 --> <Manager className="org.apache.catalina.session.PersistentManager"                                                                                 maxIdleSwap="1">  <Store className="org.apache.catalina.session.FileStore" directory="itcast205" /> </Manager></Context>
原创粉丝点击