ServletContextListener的应用

来源:互联网 发布:淘宝我的通信在哪里 编辑:程序博客网 时间:2024/06/02 02:40

          我们在web.xml文件中配置的<context-param>参数的名称和值的类型都只能是String类型的,有时候我们希望整个web app应用共享一些引用对象,这时候该如何处理呢?

     我们可以想到的方法就是 是不是可以在web容器启动后的某个时机,创建我们希望共享的引用对象,这时候就能满足整个app共享的需求了,可是是否存在这样一个时机呢?

    ServletContextListener这个类可以满足我们的需求,我们可以创建一个类,实现ServletContextListener类,我们的类就可以监听web容器启动或者消亡的事件了。

    ServletContextListener里面有两个方法,void contextInitialized(ServletContextEvent event) 和void contextDestroyed(ServletContextEvent event)。

   contextInitialized(ServletContextEvent event)用来监听容器初始化的事件。

   contextDestroyed(ServletContextEvent event)用来监听容器关闭时的事件。

   例如我们现在想在web.xml中存储一个狗的品种的信息(字符串),然后利用 ServletContextListener的实现类监听容器的启动事件,容器启动的时候,我们创建一个狗的对象存储在ServletContext中,供整个app共享。

   我们先配置web.xml文件,这时候需要配置两个信息。


    一个是初始化参数,即狗的品种

    

  <context-param>     <param-name>pinzhong</param-name>     <param-value>heihei</param-value>  </context-param>
   二:配置我们的监听器类,不然容器无法感知他的存在,启动或者消亡也就不会发送消息给他了。

   

 <listener>    <listener-class>       MyContextListener    </listener-class>  </listener>

  完整的xml文件:

   

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 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_2_5.xsd">  <display-name></display-name>  <servlet>    <servlet-name>TestListener</servlet-name>    <servlet-class>TestListener</servlet-class>  </servlet>  <servlet>    <servlet-name>TestServletLife</servlet-name>    <servlet-class>TestServletLife</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>TestListener</servlet-name>    <url-pattern>/TestListener</url-pattern>  </servlet-mapping>  <servlet-mapping>    <servlet-name>TestServletLife</servlet-name>    <url-pattern>/TestServletLife</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <context-param>     <param-name>pinzhong</param-name>     <param-value>heihei</param-value>  </context-param>    <listener>    <listener-class>       MyContextListener    </listener-class>  </listener>  </web-app>

  创建我们的Dog类,他只有一个pinzhong属性。

   

public class Dog {   public String pinzhong;    public Dog(String pinzhong) {super();this.pinzhong = pinzhong;}public String getPinzhong() {return pinzhong;}public void setPinzhong(String pinzhong) {this.pinzhong = pinzhong;}}

 创建我们的监听器类:

  

import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class MyContextListener implements ServletContextListener{//容器消亡后调用public void contextDestroyed(ServletContextEvent event) {}//容器启动后调用public void contextInitialized(ServletContextEvent event) {   ServletContext cxt = event.getServletContext();String pinzhong = cxt.getInitParameter("pinzhong");Dog dog = new Dog(pinzhong);cxt.setAttribute("d", dog);}}

  创建我们的测试类:

  

import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class TestListener extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {   doPost(request,response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {   Dog d = (Dog) this.getServletContext().getAttribute("d");   System.out.println(d.getPinzhong());   }}

  当我们在浏览器中输入servlet的地址后,tomcat容器将会输出dog的pinzhong信息,我们将会看到heihei。