下面是一个对ServletContext和其属性监听的类MyServletContextListener.java
package com.jspdev.ch8;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import java.io.*;
public final class MyServletContextListener
    implements ServletContextListener,ServletContextAttributeListener {
   
    private ServletContext context = null;    /**
     *以下代码实现ServletContextListener接口。
     */
    public void contextDestroyed(ServletContextEvent sce) { logout("contextDestroyed()-->ServletContext被销毁");
   this.context = null;    }    public void contextInitialized(ServletContextEvent sce) { this.context = sce.getServletContext();
logout("contextInitialized()-->ServletContext初始化了");    }//ServletContextListener
    
    /**
     *以下代码实现 ServletContextAttributeListener接口
     */    
    public void attributeAdded(ServletContextAttributeEvent scae) { logout("增加了一个ServletContext属性:attributeAdded('" + scae.getName() + "', '" +
    scae.getValue() + "')");    }    public void attributeRemoved(ServletContextAttributeEvent scae) { logout("删除了一个ServletContext属性:attributeRemoved('" + scae.getName() + "', '" +
    scae.getValue() + "')");    }
    public void attributeReplaced(ServletContextAttributeEvent scae) { logout("某个ServletContext的属性被改变:attributeReplaced('" + scae.getName() + "', '" +
    scae.getValue() + "')");    }    private void logout(String message) {
    
    PrintWriter out=null;
    try
    {
     out=new PrintWriter(new FileOutputStream("c:\\test.txt",true));
     out.println(new java.util.Date().toLocaleString()+"::Form ContextListener: " + message);
     out.close();
    }
    catch(Exception e)
    {
     out.close();
     e.printStackTrace();
    }    }  }MyServletContextListener实现了ServletContextListener和ServletContextAttrivuteListener接口,所以它能监听ServletContext创建,销毁及它的属性修改的信息.下面是部署这个监听器web.xml,相应的代码是
<web-app>
    <listener>
        <listener-class>com.jspdev.ch8.MyServletContextListener</listener-class>
    </listener>
</web-app>下面是一个测试程序context_test.jsp
<%out.println("add attribute");
getServletContext().setAttribute("userName","hellking");
out.println("replace attribute");
getServletContext().setAttribute("userName","asiapower");
out.println("remove attrivute");
getServletContext().removeAttribute("userName");
%>运行context_test.jsp后,在c:\test.txt中的信息应该为:
21:38 2005-9-21
2005-9-21 21:36:39::Form ContextListener: 增加了一个ServletContext属性:attributeAdded('userName', 'hellking')
2005-9-21 21:36:39::Form ContextListener: 某个ServletContext的属性被改变:attributeReplaced('userName', 'hellking')
2005-9-21 21:36:39::Form ContextListener: 删除了一个ServletContext属性:attributeRemoved('userName', 'asiapower')但是出现了错误:
2007-8-23 11:07:05 org.apache.catalina.core.StandardContextValve invoke
严重: Exception sending request initialized lifecycle event to listener instance of class com.jspdev.ch8.MyRequestListener
java.lang.LinkageError: Class javax/servlet/ServletRequestEvent violates loader constraints
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1847)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:873)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1326)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1205)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at com.jspdev.ch8.MyRequestListener.requestInitialized(MyRequestListener.java:17)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:163)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
at java.lang.Thread.run(Thread.java:595)
请各位大哥帮忙解决!