ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javaweb之Servlet(六)监听器

2021-07-06 16:04:44  阅读:120  来源: 互联网

标签:javaweb 对象 void Session 事件 监听器 Servlet public


监听器

介绍

监听器就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即被执行。

作用

监听器可以用来检测网站的在线人数,统计网站的访问量等等!

监听器组件

监听器涉及三个组件:事件源,事件对象,事件监听器

当事件源发生某个动作的时候,它会调用事件监听器的方法,并在调用事件监听器方法的时候把事件对象传递进去。

我们在监听器中就可以通过事件对象获取得到事件源,从而对事件源进行操作!
在这里插入图片描述

模拟监听器

写一个对象,被监听器监听。

监听器

监听器定义为接口,监听的方法需要事件对象传递进来,从而在监听器上通过事件对象获取得到事件源,对事件源进行修改!

/**
 * 事件监听器
 *
 * 监听Person事件源的eat和sleep方法
 */
interface PersonListener{

    void doEat(Event event);
    void doSleep(Event event);
}

事件源

事件源是一个Person类,它有eat和sleep()方法。

事件源需要注册监听器(即在事件源上关联监听器对象)

如果触发了eat或sleep()方法的时候,会调用监听器的方法,并将事件对象传递进去

/**
 *
 * 事件源Person
 *
 * 事件源要提供方法注册监听器(即在事件源上关联监听器对象)
 */

class Person {

    //在成员变量定义一个监听器对象
    private PersonListener personListener ;
    
    //在事件源中定义两个方法
    public void Eat() {
        
        //当事件源调用了Eat方法时,应该触发监听器的方法,调用监听器的方法并把事件对象传递进去
        personListener.doEat(new Event(this));
    }

    public void sleep() {

        //当事件源调用了Eat方法时,应该触发监听器的方法,调用监听器的方法并把事件对象传递进去
        personListener.doSleep(new Event(this));
    }

    //注册监听器,该类没有监听器对象啊,那么就传递进来吧。
    public void registerLister(PersonListener personListener) {
        this.personListener = personListener;
    }

}

事件对象

事件对象封装了事件源。

监听器可以从事件对象上获取得到事件源的对象(信息)

/**
  * 事件对象Even
  *
  * 事件对象封装了事件源
  *
  * 在监听器上能够通过事件对象获取得到事件源
  */
 class Event{
     private Person person;
 
     public Event() {
     }
 
     public Event(Person person) {
         this.person = person;
     }
 
     public Person getResource() {
         return person;
     }
 
 }

测试


public static void main(String[] args) {

     Person person = new Person();

     //注册监听器()
     person.registerLister(new PersonListener() {
         @Override
         public void doEat(Event event) {
             Person person1 = event.getResource();
             System.out.println(person1 + "正在吃饭呢!");
         }

         @Override
         public void doSleep(Event event) {
             Person person1 = event.getResource();
             System.out.println(person1 + "正在睡觉呢!");
         }
     });


     //当调用eat方法时,触发事件,将事件对象传递给监听器,最后监听器获得事件源,对事件源进行操作
     person.Eat();
 }

总结

  • 事件源:拥有事件
  • 监听器:监听事件源所拥有的事件(带事件对象参数的)
  • 事件对象:事件对象封装了事件源对象
    • 事件源要与监听器有关系,就得注册监听器【提供方法得到监听器对象】
    • 触发事件源的事件,实际会提交给监听器对象处理,并且把事件对象传递过去给监听器。

Servlet监听器

在Servlet规范中定义了多种类型的监听器,它们用于监听的事件源分别 ServletContext, HttpSession和ServletRequest这三个域对象

和其它事件监听器略有不同的是,servlet监听器的注册不是直接注册在事件源上,而是由WEB容器负责注册,开发人员只需在web.xml文件中使用<listener>标签配置好监听器,

监听对象的创建和销毁

HttpSessionListener、ServletContextListener、ServletRequestListener分别监控着Session、Context、Request对象的创建和销毁

  • HttpSessionListener(可以用来收集在线者信息)
  • ServletContextListener(可以获取web.xml里面的参数配置)
  • ServletRequestListener
public class Listener1 implements ServletContextListener,
        HttpSessionListener, ServletRequestListener {

    // Public constructor is required by servlet spec
    public Listener1() {
    }
    
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("容器创建了");
    }

    public void contextDestroyed(ServletContextEvent sce) {

        System.out.println("容器销毁了");
    }


    public void sessionCreated(HttpSessionEvent se) {

        System.out.println("Session创建了");
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("Session销毁了");
    }


    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
        
    }

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {

    }
}

监听器监听到ServletContext的初始化了,Session的创建和ServletContext的销毁。(服务器停掉,不代表Session就被销毁了。Session的创建是在内存中的,所以没看到Session被销毁了)

监听对象属性变化

ServletContextAttributeListener、HttpSessionAttributeListener、ServletRequestAttributeListener分别监听着Context、Session、Request对象属性的变化

这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。

  • attributeAdded()
  • attributeRemoved()
  • attributeReplaced()

监听Session内的对象

除了上面的6种Listener,还有两种Linstener监听Session内的对象,分别是HttpSessionBindingListener和HttpSessionActivationListener,实现这两个接口并不需要在web.xml文件中注册

  • 实现HttpSessionBindingListener接口,JavaBean 对象可以感知自己被绑定到 Session 中和从 Session 中删除的事件【和HttpSessionAttributeListener的作用是差不多的】
  • 实现HttpSessionActivationListener接口,JavaBean 对象可以感知自己被活化和钝化的事件(当服务器关闭时,会将Session的内容保存在硬盘上【钝化】,当服务器开启时,会将Session的内容在硬盘式重新加载【活化】)

测试

监听器和事件源

/*
* 由于涉及到了将内存的Session钝化到硬盘和用硬盘活化到内存中,所以需要实现Serializable接口
*
* 该监听器是不需要在web.xml文件中配置的。但监听器要在事件源上实现接口
* 也就是说,直接用一个类实现HttpSessionBindingListener和HttpSessionActivationListener接口是监听不到Session内对象的变化的。
* 因为它们是感知自己在Session中的变化!
* */
public class User implements HttpSessionBindingListener,HttpSessionActivationListener,Serializable {

    private String username ;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


    @Override
    public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {

        HttpSession httpSession = httpSessionEvent.getSession();

        System.out.println("钝化了");

    }

    @Override
    public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
        HttpSession httpSession = httpSessionEvent.getSession();
        System.out.println("活化了");

    }
    @Override
    public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {

        System.out.println("绑定了对象");
    }
    @Override
    public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("解除了对象");

    }
}

测试代码

 User user = new User();
request.getSession().setAttribute("aaa", user);
request.getSession().removeAttribute("aaa");

案例

踢人小案列

列出所有的在线用户,后台管理者拥有踢人的权利,点击踢人的超链接,该用户就被注销了。

1、列出在线用户
所有的在线用户就应该用一个容器(或者使用数据库)来装载所有的Session。

2、我们监听Session的是否有属性添加(监听Session的属性有添加、修改、删除三个方法。如果监听到Session添加了,那么这个肯定是个在线用户!)。

3、装载Session的容器应该是在Context里边的【属于全站点】,并且容器应该使用Map集合【待会还要通过用户的名字来把用户踢了】

思路

  • 写监听器,监听是否有属性添加在Session里边了。
  • 写简单的登陆页面。
  • 列出所有的在线用户
  • 实现踢人功能(也就是摧毁Session)

代码:

监听器


public class KickPerson implements HttpSessionAttributeListener {

    // Public constructor is required by servlet spec
    public KickPerson() {
    }

    public void attributeAdded(HttpSessionBindingEvent sbe) {

        //得到context对象,看看context对象是否有容器装载Session
        ServletContext context = sbe.getSession().getServletContext();

        //如果没有,就创建一个呗
        Map map = (Map) context.getAttribute("map");
        if (map == null) {
            map = new HashMap();
            context.setAttribute("map", map);
        }

        //---------------------------------------------------------------------------------------
        
        //得到Session属性的值
        Object o = sbe.getValue();

        //判断属性的内容是否是User对象
        if (o instanceof User) {
            User user = (User) o;
            map.put(user.getUsername(), sbe.getSession());
        }
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
    }
}

处理登录的 servlet


//得到传递过来的数据
String username = request.getParameter("username");

User user = new User();
user.setUsername(username);

//标记该用户登陆了!
request.getSession().setAttribute("user", user);

//提供界面,告诉用户登陆是否成功
request.setAttribute("message", "恭喜你,登陆成功了!");
request.getRequestDispatcher("/message.jsp").forward(request, response);

处理踢人的 servlet

String username = request.getParameter("username");

//得到装载所有的Session的容器
Map map = (Map) this.getServletContext().getAttribute("map");

//通过名字得到Session
HttpSession httpSession = (HttpSession) map.get(username);
httpSession.invalidate();
map.remove(username);

//摧毁完Session后,返回列出在线用户页面
request.getRequestDispatcher("/listUser.jsp").forward(request, response);

监听Seesion的创建和监听Session属性的变化有啥区别???

  • Session的创建只代表着浏览器给服务器发送了请求。会话建立
  • Session属性的变化就不一样了,登记的是具体用户是否做了某事(登陆、购买了某商品)

标签:javaweb,对象,void,Session,事件,监听器,Servlet,public
来源: https://blog.csdn.net/weixin_44044929/article/details/118513988

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有