与ServletContext相关的监听器

概述

  与ServletContext相关的监听器有ServletContextListenerServletContextAttributeListener


ServletContextListener

  ServletContextListener是“生命周期监听器”,可以让我们知道Web应用程序的初始化完成或即将销毁的时机。
  在Web应用程序初始化后或即将销毁前,Web容器会调用contextInitialized()或contextDestroyed(),并会传入ServletContextEvent,我们可以通过ServletContextEvent的getServletContext()方法取得ServletContext,再通过ServletContext的getInitParameter()方法来读取web应用程序参数。

demo  

  (1)ServletContextListener可以直接使用@WebListener标注来声明。

package com.test;

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

@WebListener
public class ServletContextListenerTest implements ServletContextListener {
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        String value = context.getInitParameter("testParam");
        System.out.println(value);
    }
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) { }
}
java Code
<?xml version="1.1" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
  <display-name>Archetype Created Web Application</display-name>

  <context-param>
    <param-name>testParam</param-name>
    <param-value>testValue</param-value>
  </context-param>
</web-app>
xml Code

  测试方法

  启动web应用控制台会输出:testValue

  (2)在Servlet 3.0之前,ServletContextListener实现类必须在web.xml中使用<listener>与<listener-class>标签来声明。

<?xml version="1.1" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>testParam</param-name>
    <param-value>testValue</param-value>
  </context-param>
  <listener>
    <listener-class>com.test.ServletContextListenerTest</listener-class>
  </listener>
</web-app>
View Code 

应用 

  可以在contextInitialized()中实现应用程序资源的准备动作,在contextDestroyed()实现释放应用程序资源的动作。例如,在应用程序初始过程中,准备好数据库连线对象、读取应用程序设置等动作。
  有些应用程序的设置,必须在Web应用程序初始化时进行,例如改变HttpSession的Cookie设置
  方法一、在web.xml中定义。
  方法二、在应用程序初始化时取得ServletContext后,使用getSessionCookieConfig()取得SessionCookieConfig进行设置。

@WebListener()
public class SomeContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        context.getSessionCookieConfig().setName("caterpillar-sessionId");
    }
}
View Code

ServletContextAttributeListener 

  ServletContextAttributeListener是“属性改变监听器”。当在ServletContext中添加、移除或替换属性时,可以收到通知,相对应的方法attributeAdded()、attributeRemoved()与attributeReplaced()会被调用。

package com.test;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;

@WebListener
public class ServletContextAttributeListenerTest implements ServletContextAttributeListener {

    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {
        event.getName();
        event.getValue();
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent event) {
        event.getName();
        event.getValue();
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent event) {
        event.getName();
        event.getValue();
    }
}
View Code

在web.xml中也可以声明

<?xml version="1.1" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <listener>
    <listener-class>com.test.ServletContextAttributeListenerTest</listener-class>
  </listener>
</web-app>
xml Code

 

posted @ 2018-12-03 20:05  zhuangrunwei  阅读(522)  评论(0编辑  收藏  举报