Java ---Listener监听器

作者: Single_YAM | 来源:发表于2017-04-13 09:53 被阅读182次

    在我们的web容器中,一直不断的触发着各种事件,例如:web应用启动和关闭,request请求到达和结束等。但是这些事件通常对于开发者来说是透明的,我们可以根据这些接口开发符合我们自身需求的功能。在web中常见的的几个监听事件如下:

    • ServletContextListener:用于监听web应用的启动和关闭
    • ServletContextAttributeListener:用于监听在application范围内的数据的变动
    • ServletRequestListener:用于监听用户请求的细节
    • ServletRequestAttributeListener:用于监听request范围内的数据的变动
    • HttpSessionListener:用于监听某次会话的开始和结束
    • HttpSessionAttributeListener:用于监听session范围内的属性数据的变动

    一、使用ServletContextListener监听web应用的启动和关闭
    我们想要实现一个自定义的Listener,需要两个步骤,第一个是根据自己的需求继承相应的上述的监听事件的接口,并实现其中的相应的方法。第二个步骤就是,在web.xml中配置此Listener监听器的处理类或者使用注解配置。下面我们通过继承自ServletContextListener接口来实现对web应用的启动和关闭实时监控。

    public class MyListener implements ServletContextListener {
    
        @Override//web应用启动时候触发此事件
        public void contextInitialized(ServletContextEvent sce) {
            System.out.println("web应用启动了。。。");
        }
    
        @Override//web应用关闭的时候触发事件
        public void contextDestroyed(ServletContextEvent sce) {
    
            System.out.println("web应用被关闭了。。。");
        }
    
    }
    
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
    
        <listener>
            <listener-class>Test_packge.MyListener</listener-class>
        </listener>
    </web-app>
    
    1.png

    二、使用ServletContextAttributeListener监听application范围内的属性变化
    上面的代码可以让我们监听web应用的启动和关闭情况,下面我们可以通过实现ServletContextAttributeListener接口来完成对application范围内的属性数值改变情况进行监听。其中主要有以下三个方法:

    
        default void attributeAdded(ServletContextAttributeEvent scae) {
        }
    
        default void attributeRemoved(ServletContextAttributeEvent scae) {
        }
    
        default void attributeReplaced(ServletContextAttributeEvent scae) {
        }
    

    这三个方法分别会在application范围内添加属性的时候,删除属性的时候,更新属性的时候触发。下面做一点演示:

    public class MyListener implements ServletContextAttributeListener {
    
        @Override
        public void attributeAdded(ServletContextAttributeEvent scae) {
    
            String name = scae.getName();
            Object value = scae.getValue();
            System.out.println("add: "+name+":"+value);
        }
        @Override
        public void attributeRemoved(ServletContextAttributeEvent scae) {
    
            String name = scae.getName();
            Object value = scae.getValue();
            System.out.println("remove: "+name+":"+value);
        }
        @Override
        public void attributeReplaced(ServletContextAttributeEvent scae) {
    
            String name = scae.getName();
            Object value = scae.getValue();
            System.out.println("replaced: "+name+":"+value);
        }
    }
    
    <%application.setAttribute("x","a");%>
    
    <%application.removeAttribute("x","a");%>
    

    上述代码中,我们调用了ServletContextAttributeEvent 对象的getName方法和getValue方法来获取当前的application属性的名和值。当然此对象还有另外一个方法就是用于获取servlecontext对象的。

    三、使用ServletRequestListener监听用户请求的开始和结束
    上述代码主要完成的是对web应用的启动和结束的状态进行监听,包括application范围内的属性值的改变的监听。下面我们看对用户请求进行监听的接口ServletRequestListener,它主要有两个方法:

        default void requestDestroyed(ServletRequestEvent sre) {
        }
    
        default void requestInitialized(ServletRequestEvent sre) {
        }
    

    这两个方法分别会在请求的到达和请求的结束时触发,ServletRequestEvent 中定义的方法可以返回当前请求的对象request和web应用的ServletContext对象。通过继承接口ServletRequestAttributeListener可以实现监听request范围内的属性的变更,其内部方法和上述的ServletContextAttributeListener 类似,不演示了。

    四、使用HttpSessionListener监听会话的开始和结束
    HttpSessionListener监听器用于监听每个用户创建会话和关闭会话的动作,有两个方法:

        default void sessionCreated(HttpSessionEvent se) {
        }
    
        default void sessionDestroyed(HttpSessionEvent se) {
        }
    

    一个是用于监听会话开始,一个是用于监听会话的结束。HttpSessionEvent 类可以返回一HttpSession对象,该对象指向当前会话,可以从中获取会话创建的时间,属性值,sessionId,ServletContext对象等。通过他我们可以直接的监视具体用户的各个操作。下面我们通过一个例子:显示当前所有在线的用户

    public class MyListener implements HttpSessionListener {
    
        public void sessionCreated(HttpSessionEvent se) {
    
            ServletContext app = se.getSession().getServletContext();
            //如果是新一次会话
            if(se.getSession().isNew()){
                Map<String,String> map = (Map<String,String>)app.getAttribute("online");
                if(map==null){
                    //如果这是第一次会话,创建一个用于记录在线人数的map集合
                    map = new Hashtable<String, String>();
                }
                //此处的用户名实际上是可以获取的,此处为了演示方便,默认walekr
                map.put(se.getSession().getId(),"walker");
                app.setAttribute("online",map);
            }
        }
        public void sessionDestroyed(HttpSessionEvent se) {
    
            ServletContext app = se.getSession().getServletContext();
            Map<String,String> map = (Map<String,String>)app.getAttribute("online");
            if(map!=null) {
                map.remove(se.getSession().getId());
            }
            app.setAttribute("online",map);
        }
    }
    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
      <head>
        <title></title>
      </head>
      <body>
          <%
            Map<String,String> map = (Map<String,String>)application.getAttribute("online");
            for(String key : map.keySet()){
          %>
          <p><%=key%>:<%=map.get(key)%></p>
        <%}%>
      </body>
    </html>
    
    2.png

    做一点解释,当用户使用浏览器访问服务器时候,会为此用户创建session对象,我们监听创建session动作,在application范围内创建map映射集合用于记录sessionId和用户名,sessionId是web容器为我们自动创建的不会重复的序列。我们可以打开多个浏览器模拟多用户访问,可以看到结果如上图所示,输出了所有与在线的用户sessionId和用户名。需要注意一点的是,session对象在服务器端存在的是时间是有限的,也就是说如果某个用户长时间没有再次访问服务器更新session的话,服务器会自动清除该对象,也就导致用户可能需要重新登录。还有一点,每个客户端都会获取在服务器端的session对象,但是如果客户端关闭了,session并不会立即从服务器端清除,依然需要等到超时之后服务器删除该对象。具体的深入理解session,以后的文章会介绍,此处了解即可。

    此外,HttpSessionAttributeListener可以用来监听session范围内的属性的变更,主要有如下几个方法:

        default void attributeAdded(HttpSessionBindingEvent se) {
        }
    
        default void attributeRemoved(HttpSessionBindingEvent se) {
        }
    
        default void attributeReplaced(HttpSessionBindingEvent se) {
        }
    

    这三个方法和上述的几个监听属性的方法是类似的,此处不再赘述。

    以上便是Listener监听器的基本内容,理解较为浅薄,望对大家有帮助!

    相关文章

      网友评论

        本文标题:Java ---Listener监听器

        本文链接:https://www.haomeiwen.com/subject/rbxmattx.html