• j2ee servlet listener


    JSP/Servlet 中的事件处理写过AWT或Swing程序的人一定对桌面程序的事件处理机制印象深刻:通过实现Listener接口的类可以在特定事件(Event)发生时,呼叫特定的方法来对事件进行响应。

    其实我们在编写JSP/Servle程序时,也有类似的事件处理机制,所不同的是在JSP/Servlet中是在web.xml中注册Listener,由Container在特定事件发生时呼叫特定的实现Listener的类。


    1. Servlet中的Listener和Event:

    在JSP 2.0/Servlet 2.4中,共有八个Listener接口,六个Event类别。 
    ServletContextListener接口 
    [接口方法] contextInitialized()与 contextDestroyed() 
    [接收事件] ServletContextEvent 
    [触发场景] 在Container加载Web应用程序时(例如启动 Container之后),会呼叫contextInitialized(),而当容器移除Web应用程序时,会呼叫contextDestroyed ()方法。

    ServletContextAttributeListener 
    [接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved() 
    [接收事件] ServletContextAttributeEvent 
    [触发场景] 若有对象加入为application(ServletContext)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、attributeRemoved()。

    HttpSessionListener 
    [接口方法] sessionCreated()与sessionDestroyed () 
    [接收事件] HttpSessionEvent 
    [触发场景] 在session (HttpSession)对象建立或被消灭时,会分别呼叫这两个方法。

    HttpSessionAttributeListener 
    [接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved() 
    [接收事件] HttpSessionBindingEvent 
    [触发场景] 若有对象加入为session(HttpSession)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。

    HttpSessionActivationListener 
    [接口方法] sessionDidActivate()与 sessionWillPassivate() 
    [接收事件] HttpSessionEvent 
    [触发场景] Activate与Passivate是用于置换对象的动作,当session对象为了资源利用或负载平衡等原因而必须暂时储存至硬盘或其它储存器时(透过对象序列化),所作的动作称之为Passivate,而硬盘或储存器上的session对象重新加载JVM时所采的动作称之为Activate,所以容易理解的,sessionDidActivate()与 sessionWillPassivate()分别于Activeate后与将Passivate前呼叫。

    ServletRequestListener 
    [接口方法] requestInitialized()与 requestDestroyed() 
    [接收事件] RequestEvent 
    [触发场景] 在request(HttpServletRequest)对象建立或被消灭时,会分别呼叫这两个方法。

    ServletRequestAttributeListener 
    [接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved() 
    [接收事件] HttpSessionBindingEvent 
    [触发场景] 若有对象加入为request(HttpServletRequest)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。

    HttpSessionBindingListener 
    [接口方法] valueBound()与valueUnbound() 
    [接收事件] HttpSessionBindingEvent 
    [触发场景] 实现HttpSessionBindingListener接口的类别,其实例如果被加入至session(HttpSession)对象的属性中,则会呼叫 valueBound(),如果被从session(HttpSession)对象的属性中移除,则会呼叫valueUnbound(),实现HttpSessionBindingListener接口的类别不需在web.xml中设定。

    2. 如何注册Servlet中的事件 
    实现上面这几个接口的类别,除了HttpSessionBindingListener外,必须在web.xml中向容器注册,容器才会在对应的事件发生时呼叫对应的类别.如:

    <listener>
          <listener-class>com.listener.MyServletContextListener</listener-class>
      </listener>

    Listener采用了观察者模式 ,Listener是servlet的监听器,他可以监听客户端的请求、服务器端的操作等, 通过监听器,可以自动激发一些操作。比如:监听在线用户数量

    当增加一个HttpSession时,就会激发sessinCreated(HttpSessionEvent sce)方法,这样就可以给在线人数+1了。

    常见的监听器接口:

    ServletContextAttributeListener 监听对servletContext属性的操作,比如删除、增加、修改属性等

    ServletContextListener 监听ServletContext,当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法,当销毁ServletContext时,激发ContextDestory(ServletContextEvent sce)方法、

    实例:

    首先配置web.xml 

    <!--servlet 监听器  start-->
      <listener>
          <listener-class>com.listener.MyServletContextListener</listener-class>
      </listener>
      
      <listener>
          <listener-class>com.listener.MyServletContextAttributeListener</listener-class>
      </listener>
      <!-- servlet 监听器 end -->
    
    web.xml

    2.ServletContextListener接口的调用

    /**
     * 在xml中配置监听器
     * ServletContextListener 坚挺servletContext,当创建servletContext时,激发ContextInitialized(ServletContextEvent sce)方法
     * 当销毁servletContext时,激发contextDestoryed(ServletContextEvent sce)方法
     */
    package com.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    
    public class MyServletContextListener implements ServletContextListener
    {
    
        @Override
        public void contextDestroyed(ServletContextEvent arg0)
        {
            System.out.println(arg0.toString());
            
        }
    
        @Override
        public void contextInitialized(ServletContextEvent arg0)
        {
            System.out.println(arg0.toString());
        }
        
    }
    
    MyServletContextListener

    3.ServletContextAttributeListener接口的调用

    package com.listener;
    
    import javax.servlet.ServletContextAttributeEvent;
    import javax.servlet.ServletContextAttributeListener;
    
    public class MyServletContextAttributeListener implements ServletContextAttributeListener
    {
    
        @Override
        public void attributeAdded(ServletContextAttributeEvent arg0)
        {
            System.out.println("attributeAdd ");
            System.out.println(arg0.getName() + ":" + arg0.getValue());
            
        }
    
        @Override
        public void attributeRemoved(ServletContextAttributeEvent arg0)
        {
            System.out.println("attributeRemoved");
            System.out.println(arg0.getName() + ":" + arg0.getValue());//value值将显示之前的 值
        }
    
        @Override
        public void attributeReplaced(ServletContextAttributeEvent arg0)
        {
            System.out.println("attributeRepaced");
            System.out.println(arg0.getName() + ":" + arg0.getValue());//value值将显示之前的 值
            
        }
        
    }
    
    
    MyServletContextAttributeListener

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    
    <%
    
        application.setAttribute("name1","value1");
        application.setAttribute("name2","value2");
        application.setAttribute("name1","value3");
        application.setAttribute("name2","value4");
    %>
    <a href="listener2.jsp">next</a>
    </body>
    </html>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <%
        application.removeAttribute("name1");
    %>
    </body>
    </html>
  • 相关阅读:
    74HC165并转串级联芯片学习记录
    道砟电阻 钢轨阻抗 列车分路电阻
    电压的有效值、平均值与峰值
    铁路信号继电器
    C语言语法记录
    程序编译过程中错误记录
    min-max容斥
    矩阵树定理
    题解 SP1812 【LCS2
    杜教筛
  • 原文地址:https://www.cnblogs.com/youxin/p/5329243.html
Copyright © 2020-2023  润新知