zoukankan      html  css  js  c++  java
  • 学习java web中的listener

    web.xml里的顺序为:context-param->listener->filter->servlet

    监听器是需要新建一个类,然后按监听的对象继承:ServletContext、HttpSession、ServletRequest中的一个


    监听ServletContext生命周期的Listener

     //ServletContext  lifecycle changes
    public interface ServletContextListener extends EventListener {
    
        //initialized
        public void contextInitialized(ServletContextEvent sce);
    
     //destroyed
        public void contextDestroyed(ServletContextEvent sce);
    }

    监听ServletContext属性的Listener

    public interface ServletContextAttributeListener extends EventListener {
    
        /**
         * Receives notification that an attribute has been added to the
         * ServletContext.
         *
         * @param event the ServletContextAttributeEvent containing the
         * ServletContext to which the attribute was added, along with the
         * attribute name and value
         */
        public void attributeAdded(ServletContextAttributeEvent event);
    
        /**
         * Receives notification that an attribute has been removed
         * from the ServletContext.
         *
         * @param event the ServletContextAttributeEvent containing the
         * ServletContext from which the attribute was removed, along with
         * the attribute name and value
         */
        public void attributeRemoved(ServletContextAttributeEvent event);
    
        /*
         * Receives notification that an attribute has been replaced
         * in the ServletContext.
         *
         * @param event the ServletContextAttributeEvent containing the
         * ServletContext in which the attribute was replaced, along with
         * the attribute name and its old value
         */
        public void attributeReplaced(ServletContextAttributeEvent event);
    }

    监听HttpSession生命周期的Listener

    //about HttpSession lifecycle changes.
    public interface HttpSessionListener extends EventListener {
        
        /** 
         * Receives notification that a session has been created.
         *
         * @param se the HttpSessionEvent containing the session
         */
        public void sessionCreated(HttpSessionEvent se);
        
        /** 
         * Receives notification that a session is about to be invalidated.
         *
         * @param se the HttpSessionEvent containing the session
         */
        public void sessionDestroyed(HttpSessionEvent se);
        
    }

    监听HttpSession属性的Listener

    public interface HttpSessionAttributeListener extends EventListener {
    
        /**
         * Receives notification that an attribute has been added to a
         * session.
         *
         * @param event the HttpSessionBindingEvent containing the session
         * and the name and value of the attribute that was added
         */
        public void attributeAdded(HttpSessionBindingEvent event);
    
        /**
         * Receives notification that an attribute has been removed from a
         * session.
         *
         * @param event the HttpSessionBindingEvent containing the session
         * and the name and value of the attribute that was removed
         */
        public void attributeRemoved(HttpSessionBindingEvent event);
    
        /**
         * Receives notification that an attribute has been replaced in a
         * session.
         *
         * @param event the HttpSessionBindingEvent containing the session
         * and the name and (old) value of the attribute that was replaced
         */
        public void attributeReplaced(HttpSessionBindingEvent event);
    
    }

    监听ServletReques生命周期的tListener

    public interface ServletRequestListener extends EventListener {
    
        /**
         * Receives notification that a ServletRequest is about to go out
         * of scope of the web application.
         *
         * @param sre the ServletRequestEvent containing the ServletRequest
         * and the ServletContext representing the web application
         */
        public void requestDestroyed(ServletRequestEvent sre);
    
        /**
         * Receives notification that a ServletRequest is about to come
         * into scope of the web application.
         *
         * @param sre the ServletRequestEvent containing the ServletRequest
         * and the ServletContext representing the web application
         */
        public void requestInitialized(ServletRequestEvent sre);
    }

    监听ServletReques属性的Listener

    public interface ServletRequestAttributeListener extends EventListener {
    
        /**
         * Receives notification that an attribute has been added to the
         * ServletRequest.
         *
         * @param srae the ServletRequestAttributeEvent containing the 
         * ServletRequest and the name and value of the attribute that was
         * added
         */
        public void attributeAdded(ServletRequestAttributeEvent srae);
    
        /**
         * Receives notification that an attribute has been removed from the
         * ServletRequest.
         *
         * @param srae the ServletRequestAttributeEvent containing the 
         * ServletRequest and the name and value of the attribute that was
         * removed
         */
        public void attributeRemoved(ServletRequestAttributeEvent srae);
    
        /**
         * Receives notification that an attribute has been replaced on the
         * ServletRequest.
         *
         * @param srae the ServletRequestAttributeEvent containing the 
         * ServletRequest and the name and (old) value of the attribute
         * that was replaced
         */
        public void attributeReplaced(ServletRequestAttributeEvent srae);
    }

    HttpSessionBindingListener:该接口有两个方法,valueBound和valueUnbound,实现该接口的对象要new以后放在session里。

    HttpSessionActivationListener :实现了HttpSessionActivationListener接口的JavaBean对象可以感知自己被活化(反序列化)sessionDidActivate和钝化(序列化)sessionWillPassivate的事件

    HttpSessionBindingListener和HttpSessionListener区别是前者要创建对象后放入session中,后者创建一次

    ...
  • 相关阅读:
    UnQLite简介
    .net中webconfig自定义配置
    webservice有关application/xop+xml的异常
    .NET4缓存过期策略摘录
    关于sea.js的笔记
    npm笔记和bower
    使用npm安装一些包失败了的看过来(npm国内镜像介绍)
    easyui的datagird动态设置当前页数
    Oracle中Merge into用法总结
    Oracle 12.1.0.2 对JSON的支持
  • 原文地址:https://www.cnblogs.com/javage/p/9304804.html
Copyright © 2011-2022 走看看