Listener监听器----ServletContext的生命周期监听器

一、Listener监听器

  监听器用于监听web应用中某些对象的创建、销毁、增加、修改删除等动作的发生,然后做出相应的相应处理,当范围对象的状态发生变化的时候,服务器会自动调用监听器对象中的方法

二、监听器分类

按监听的对象划分,可以分为

ServletContext对象生命周期监听器与属性操作监听器

HttpSession对象生命周期监听器与属性操作监听器

ServletRequest对象声明周期监听器与属性操作监听器

三、ServletContext对象的生命周期监听器

 ServletContextListener接口定义了ServletContext对象生命周期的监听行为

 void contextInitialized(ServletContextEvent sce)

    ServletContext 对象创建以后,会触发该监听方法,并将ServletContext对象传递到该方法中

void contextDestoryed(ServletContextEvent sce)

    ServletContext对象在销毁之前会触发该监听方法,并将ServletContext对象传递到该方法中。

四、创建能够监听ServletContext生命周期的监听器

web.xml:

    
        cn.itcast.listener.ServletContextLifecycleListener
    

实现类:

package cn.itcast.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * ServletContext对象生命周期监听器
 */
public class ServletContextLifecycleListener implements ServletContextListener {
    /*
     监听ServletContext 对象创建的监听方法
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext sc = sce.getServletContext();
        System.out.println(sc);
        System.out.println("ServletContext init.........");
    }
    /*
        监听ServletContext 对象销毁的监听方法
        */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext sc = sce.getServletContext();
        System.out.println(sc);
        System.out.println("ServletContext destory.........");
    }
}

 五、ServletContext对象的属性操作监听器

ServletContextAttributeListener接口定义了对于ServletContext对象属性操作的监听行为。

void attributeAdded(ServletContextAttributeEvent scae)

        向ServletContext对象中添加属性时会触发该监听方法,并将ServletContext对象传递到该方法当中,触发事件的方法servletContext.setAttribute("key","value")。

void attributeRemoved(ServletContextAttributeEvent scae)

        当从ServletContext对象中删除属性时会触发该监听方法,并将ServletContext传递到该方法中。触发事件方法servletContext.removeAttribute("key")

void attributeReplaced(ServletContextAttributeEvent scae)

        当从ServletContext对象中属性的值发生替换时会触发该监听方法,并将ServletContext传递到该方法中。触发事件方法servletContext.setAttribute("key","value")

实例代码:

servlet:

package cn.itcast.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletContextAttrServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        servletContext.setAttribute("key","test");
        servletContext.setAttribute("key","test2");
        servletContext.removeAttribute("key");
    }
}

 监听:

package cn.itcast.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

/**
 * servletContext 属性操作监听器
 */
public class ServletContextAttrListener implements ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        System.out.println("----------start attributeAdded----------");
        System.out.println("Name: "+scae.getName()+" Value:"+scae.getValue());
        System.out.println("----------end attributeAdded----------");
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        System.out.println("----------start attributeRemoved----------");
        System.out.println("Name: "+scae.getName()+" Value:"+scae.getValue());
        System.out.println("----------end attributeRemoved----------");
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        System.out.println("----------start attributeReplaced----------");
        System.out.println("Name: "+scae.getName()+" Value:"+scae.getValue());
        System.out.println("----------end attributeReplaced----------");
    }
}

输出结果:

----------start attributeAdded---------
Name:key  Value:test
----------end attributeAdded----------

----------start attributeReplaced----------

Name:key  Value:test
----------end attributeReplaced----------

----------start attributeRemoved----------
Name:key  Value:test2
----------end attributeRemoved----------

    

你可能感兴趣的:(监听器,java,前端,服务器)