码迷,mamicode.com
首页 > 编程语言 > 详细

Javaweb之Listener学习

时间:2015-04-28 11:54:12      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:listener   javaee   servlet   

监听器:主要用来监听特定对象的创建或销毁,属性的变化
          监听器是一个实现特定接口的普通Java类
  监听的对象必须要对程序的实现或数据的保存有帮助
 
  监听器在访问其监听的对象的时候由服务器创建 访问结束后由服务器销毁

 某一类监听器配置以后, 该监听器对该类对象都会进行监听


  对象的创建方式:
          对象的创建原则:谁创建,谁销毁
          1.自己创建 自己使用     不需要监听
          2.别人创建  自己用        需要监听
 
  监听器可以从属性上分为三类
    一、生命周期监听器
        监听对象的创建、销毁的过程
    二、属性监听器
        监听对象属性的添加、修改、移除的过程
    三、行为监听器
        监听对象事件的绑定或者序列化
     
 面向接口编程:不需要关注接口的具体实现,只需要关注接口具有哪些功能可以使用。
 
  Servlet中那些对象需要监听?
      域对象
          request           request监听器                                     监听request对象的创建和销毁
          session              session相关监听器            监听session对象的创建和销毁
          servletContext      servletContext监听器        监听servletContex对象的创建和销毁
          application          application监听器        监听application对象的创建和销毁
   监听器的接口:监听对象创建或销毁的监听接口
      1.ServletRequestListener 监听request对象的创建或销毁
           监听器中的方法
              void requestDestroyed(ServletRequestEvent sre)
                  The request is about to go out of scope of the web application.
             void requestInitialized(ServletRequestEvent sre)
                  The request is about to come into scope of the web application.
         2.    session相关监听器    监听session对象的创建或销毁
         HttpSessionListener
              void sessionCreated(HttpSessionEvent se)
                  Notification that a session was created.
              void sessionDestroyed(HttpSessionEvent se)
                  Notification that a session is about to be invalidated.
               session相关监听器  
              2.1HttpSessionBindingListener  监听绑定在session上的事件
                   void valueBound(HttpSessionBindingEvent event)
                      Notifies the object that it is being bound to a session and identifies the session.
                  void valueUnbound(HttpSessionBindingEvent event)
                      Notifies the object that it is being unbound from a session and identifies the session.

            2.2HttpSessionActivationListener 监听session的序列化及反序列化的监听器
                   void sessionDidActivate(HttpSessionEvent se)
                      Notification that the session has just been activated.
                  void sessionWillPassivate(HttpSessionEvent se)
                      Notification that the session is about to be passivated.
          3.ServletContextListener  监听servletContex对象的创建或销毁
                   void contextDestroyed(ServletContextEvent sce)
                      Notification that the servlet context is about to be shut down.
                  void contextInitialized(ServletContextEvent sce)
                      Notification that the web application initialization process is starting.

    监听对象属性变化的监听器接口
        1.ServletRequestAttributeListener  监听request对象属性变化(添加、移除、修改)的监听器
            void attributeAdded(ServletRequestAttributeEvent srae)
                  Notification that a new attribute was added to the servlet request.
             void attributeRemoved(ServletRequestAttributeEvent srae)
                  Notification that an existing attribute has been removed from the servlet request.
             void attributeReplaced(ServletRequestAttributeEvent srae)
                  Notification that an attribute was replaced on the servlet request.

        2.HttpSessionAttributeListener  监听session对象属性变化(添加、移除、修改)的监听器
            void attributeAdded(HttpSessionBindingEvent se)
                  Notification that an attribute has been added to a session.
             void attributeRemoved(HttpSessionBindingEvent se)
                  Notification that an attribute has been removed from a session.
             void attributeReplaced(HttpSessionBindingEvent se)
                  Notification that an attribute has been replaced in a session.

        3.ServletContextAttributeListener  监听servletContext对象的创建或销毁
            void attributeAdded(ServletContextAttributeEvent scab)
                  Notification that a new attribute was added to the servlet context.
             void attributeRemoved(ServletContextAttributeEvent scab)
                  Notification that an existing attribute has been removed from the servlet context.
             void attributeReplaced(ServletContextAttributeEvent scab)
                  Notification that an attribute on the servlet context has been replaced.

    监听器的开发步骤:
    1.写一个普通Java类,实现相关的接口
    2.在web.xml(部署描述文件)配置中配置监听器
          <!-- 监听request对象的创建、销毁 -->
          <listener>
              <listener-class>cn.test.lister.ListenerDemo</listener-class>    
          </listener>
    
 

package cn.test.lister;
/***
 * 监听request对象的创建或者销毁
 * @author ning
 *
 */
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

public class ListenerDemo implements ServletRequestListener{
    /**
     * 对象销毁
     *
     */
    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        // TODO Auto-generated method stub
        //在销毁之前可以获取request中的属性值
        System.out.println("ListenerDemo is requestDestroyed");
    }
    /**
     * 对象创建
     *
     */
    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        // TODO Auto-generated method stub
        System.out.println("ListenerDemo is requestInitialized");
    }

}

Javaweb之Listener学习

标签:listener   javaee   servlet   

原文地址:http://blog.csdn.net/ning_xian_hong/article/details/45332321

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!