监听器

监听器存在以下对象
监听者:XxxxxListener - 所的监听者是的接口
被监听者 :任意对象都可以成为被监听者 - 早已经存在
监听到的事件:XxxxEvent- 永远是一个具体类,用来放监听到的数据
里面都有一个方法叫getSource() – 返回的是监听到对象

观察者模式:

 1 public class TestObersver {
 2     public static void main(String[] args) {
 3         Person person = new Person();//声明被观察者
 4         System.err.println("pp:"+person);
 5         person.addPersonListener(new PersonListener() {
 6             public void running(PersonEvent pe) {
 7                 System.err.println("你正在跑....."+pe.getSource());
 8                 throw new RuntimeException("他跑了。。。");
 9             }
10         });
11         person.run();
12     }
13 }
14 class Person{
15     private PersonListener pl;
16     public void addPersonListener(PersonListener pl){
17         this.pl = pl;
18     }
19     public void run(){
20         if(pl!=null){
21             pl.running(new PersonEvent(this));
22         }
23         System.err.println("我正在跑步......");
24     }
25 }
26 interface PersonListener{
27     void running(PersonEvent pe);
28 }
29 class PersonEvent{
30     private Object src;
31     public PersonEvent(Object obj) {
32         this.src=obj;
33     }
34     public Object getSource(){
35         return src;
36     }
37 }

在Javaweb中存在三个被监听对象:
HttpServletRequest
HttpSessoin
ServletContext

1:配置一个Listener只要求提供类名就可以了
2:在tomcat启动时,会自动的初始化这个监听器类
3:tomcat创建的Listner,Serlvet,Filter都是单例的

 1 /**
 2  * 以下实现在线人数的统计
 3  */
 4 public class MySessionListener implements HttpSessionListener {
 5     private Integer online=1;
 6     //request.getSession();
 7     public void sessionCreated(HttpSessionEvent se) {
 8         System.err.println("有人访问本网点了");
 9         HttpSession hs1 = (HttpSession) se.getSource();
10         HttpSession hs2 = se.getSession();
11         System.err.println("hs1:"+hs1.getId()+","+hs2.getId());
12         //获取整个域的对象
13         ServletContext sc= se.getSession().getServletContext();
14         //将所有session放到servletContext
15         sc.setAttribute("online",online++);
16         
17         //先从application获取所有已经维护的sesison
18         List<HttpSession> list = (List<HttpSession>) sc.getAttribute("sessions");
19         if(list==null){//第一个访问的人
20             list = new ArrayList<HttpSession>();
21             sc.setAttribute("sessions", list);
22         }
23         list.add(hs2);
24     }
25     //过期(30)或者s.invalidate();
26     public void sessionDestroyed(HttpSessionEvent se) {
27         System.err.println("有人退出了..."+se.getSession().getId());
28         ServletContext sc= se.getSession().getServletContext();
29         sc.setAttribute("online",online--);
30     }
31 }

HttpSessionAttributeListener (重点类)

 1 public class AttributeListener implements HttpSessionAttributeListener {
 2     //sessoin.setAttribute("addr",中国北京);
 3     public void attributeAdded(HttpSessionBindingEvent e) {
 4         String name = e.getName();//--addr
 5         Object value = e.getValue();//获取sesion的value值
 6         System.err.println("添加了一个新的属性:"+name+","+value);
 7     }
 8     //session.removeAttribute("addr"); - tomcat容器
 9     public void attributeRemoved(HttpSessionBindingEvent e){
10         String name = e.getName();
11         Object value = e.getValue();
12         System.err.println("删除了一个属性:"+name+","+value);
13     }
14     //sessoin.setAttribute("addr",中国北京);
15     //sessoin.setAttribute("addr",上海);
16     public void attributeReplaced(HttpSessionBindingEvent e) {
17         String name = e.getName();
18         Object oldValue = e.getValue();
19         HttpSession session = e.getSession();
20         Object newValue = session.getAttribute(name); 
21         System.err.println("重新设置了一个值:"+name+","+oldValue+",newValue:"+newValue);    
22     }
23 }
原文地址:https://www.cnblogs.com/ylfeiu/p/3614626.html