• servlet中的listener


    说说servlet的一些监听器,这些监听器的用于就不用我说了。比如:在很多社区网站中看到的在线用户的统计就是基于此来实现的。

    入正题:

    从作用域范围来说,Servlet的作用域有ServletContext,HttpSession,ServletRequest.

    Context范围:

    ServletContextListener: 对一个应用进行全局监听.随应用启动而启动,随应用消失而消失主要有两个方法:
    1〉contextDestroyed(ServletContextEvent event)
    在应用关闭的时候调用
    2〉contextInitialized(ServletContextEvent event)
    在应用启动的时候调用

    这个监听器主要用于一些随着应用启动而要完成的工作,也就是很多人说的我想在容器启动的时候干
    一般来说对"全局变量"初始化,如
    Java代码
    1. public void contextInitialized(ServletContextEvent event){   
    2.     ServletContex sc = event.getServletContext();   
    3.     sc.setAttribute(name,value);   
    4. }   
    public void contextInitialized(ServletContextEvent event){      ServletContex sc = event.getServletContext();      sc.setAttribute(name,value);  } 

    以后你就可以在任何servlet中getServletContext().getAttribute(name);
    我最喜欢用它来做守护性工作,就是在contextInitialized(ServletContextEvent event)方法中实现一个Timer,然后就让应用在每次启动的时候让这个Timer工作:

    Java代码
    1. public void contextInitialized(ServletContextEvent event){   
    2. timer = new Timer();   
    3. timer.schedule(new TimerTask(){   
    4. public void run(){   
    5. //do any things   
    6. }   
    7. },0,时间间隔);   
    8. }   
    public void contextInitialized(ServletContextEvent event){  timer = new Timer();  timer.schedule(new TimerTask(){  public void run(){  //do any things  }  },0,时间间隔);  } 


    有人说Timer只能规定从现在开始的多长时间后,每隔多久做一次事或在什么时间做一次事,那我想在每月1号或每天12点做一项工作如何做呢?你 只要设一个间隔,然后每次判断一下当时是不是那个时间段就行了啊,比如每月一号做,那你时间间隔设为天,即24小时一个循环,然后在run方法中判断当时 日期new Date().getDate()==1就行了啊.如果是每天的12点,那你时间间隔设为小时,然后在run中判断new Date().getHour()==12,再做某事就行了.

    ServletContextAttributeListener:
    这个监听器主要监听ServletContex对象在setAttribute()和removeAttribute()的事件,注意也就是一个"全局变量"在被Add(第一次set),replace(对已有的变量重新赋值)和remove的时候.
    分别调用下面三个方法:
    public void attributeAdded(ServletContextAttributeEvent scab)这个方法不仅可以知道哪些全局变量被加进来,而且可获取容器在启动时自动设置了哪些context变量:

    Java代码
    1. public void attributeAdded(ServletContextAttributeEvent scab){   
    2.     System.out.println(scab.getName());   
    3. }   
    4. public void attributeRemoved(ServletContextAttributeEvent scab)   
    5.   
    6. public void attributeReplaced(ServletContextAttributeEvent scab)   
    public void attributeAdded(ServletContextAttributeEvent scab){      System.out.println(scab.getName());  }  public void attributeRemoved(ServletContextAttributeEvent scab)   public void attributeReplaced(ServletContextAttributeEvent scab) 


    Session范围:
    HttpSessionListener:
    这个监听器主要监听一个Session对象被生成和销毁时发生的事件.对应有两个方法:
    public void sessionCreated(HttpSessionEvent se)

    public void sessionDestroyed(HttpSessionEvent se)

    一般来说,一个session对象被create时,可以说明有一个新客端进入.可以用来粗略统计在线人数,注意这不是精确的,因为这个客户端可能立即就关闭了,但sessionDestroyed方法却会按一定的策略很久以后才会发生.

    HttpSessionAttributeListener:
    和ServletContextAttributeListener一样,它监听一个session对象的Attribut被Add(一个特定名 称的Attribute每一次被设置),replace(已有名称的Attribute的值被重设)和remove时的事件.对就的有三个方法.
    public void attributeAdded(HttpSessionBindingEvent se)

    public void attributeRemoved(HttpSessionBindingEvent se)

    public void attributeReplaced(HttpSessionBindingEvent se)

    上面的几个监听器的方法,都是在监听应用逻辑中servlet逻辑中发生了什么事,一般的说.
    我们只要完成逻辑功能,比如session.setAttribute("aaa","111");我只要把一个名为aaa的变量放在 session中以便以后我能获取它,我并不关心当session.setAttribute("aaa","111");发生时我还要干什么.(当然有 些时候要利用的),但对于下面这个监听器,你应该好好发解一下:
    HttpSessionBindingListener: 上面的监听器都是作为一个独立的Listener在容器中控制事件的.而HttpSessionBindingListener对在一对象中监听该对象的 状态,实现了该接口的对象如果被作为value被add到一个session中或从session中remove,它就会知道自己已经作为一个 session对象或已经从session删除,这对于一些非纯JAVA对象,生命周期长于session的对象,以及其它需要释放资源或改变状态的对象 非常重要.

    比如:
    session.setAttribute("abcd","1111");
    以后session.removeAttribute("abcd");
    因为abcd是一个字符中,你从session中remove后,它就会自动被垃圾回收器回收,而如果是一个connection:(只是举例,你千万不要加connection往session中加入)
    session.setAttribute("abcd",conn);
    以后session.removeAttribute("abcd");这时这个conn被从session中remove了,你已经无法获取它 的句柄,所以你根本没法关闭它.而在没有remove之前你根本不知道什么时候要被remove,你又无法close(),那么这个connection 对象就死了.另外还有一些对象可以在被加入一个session时要锁定还要被remove时要解锁,应因你在程序中无法判断什么时候被 remove(),add还好操作,我可以先加锁再add,但remove就后你就找不到它的句柄了,根本没法解锁,所以这些操作只能在对象自身中实现.
    也就是在对象被add时或remove时通知对象自己回调相应的方法:

    Java代码
    1. MyConn extends Connection implements HttpSessionBindingListener{   
    2. public void valueBound(HttpSessionBindingEvent se){   
    3. this.initXXX();   
    4. }   
    5. public void valueUnbound(HttpSessionBindingEvent se){   
    6.   
    7. this.close();   
    8. }   
    9. }   
    MyConn extends Connection implements HttpSessionBindingListener{  public void valueBound(HttpSessionBindingEvent se){  this.initXXX();  }  public void valueUnbound(HttpSessionBindingEvent se){   this.close();  }  } 


    session.setAttribute("aaa",new MyConn());
    这时如果调用session.removeAttribute("aaa"),则触发valueUnbound方法,就会自动关闭自己.
    而其它的需要改变状态的对象了是一样.
  • 相关阅读:
    宝塔跨域
    朵纳接口
    thinkphp5笔记
    phpstudy 配置域名访问无效
    如何在CentOS 下开放8080端口
    PHP:cURL error 60: SSL certificate unable to get local issuer certificate
    thinkphp6 项目使用composer安装好后提示控制器不存在:appcontrollerIndex
    DB2和MySql的区别是什么?
    idea 编辑工具
    Sublime text3最全快捷键清单
  • 原文地址:https://www.cnblogs.com/wycg1984/p/1580443.html
Copyright © 2020-2023  润新知