Error configuring application listener of class问题
我在调试“java web中使用 Listener”,在web.xml中增加
<listener>
<listener-class>MyServlet.OnLineCountListener</listener-class>
</listener>
就报错。如下:
[code=text]2013-2-22 15:58:24 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/lib/i386/server:/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/lib/i386:/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/../lib/i386:/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/lib/i386/client:/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/lib/i386::/usr/java/packages/lib/i386:/lib:/usr/lib
2013-2-22 15:58:24 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:ServletTest' did not find a matching property.
2013-2-22 15:58:24 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8080"]
2013-2-22 15:58:24 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-bio-8009"]
2013-2-22 15:58:24 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 579 ms
2013-2-22 15:58:24 org.apache.catalina.core.StandardService startInternal
信息: Starting service Catalina
2013-2-22 15:58:24 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.34
2013-2-22 15:58:25 org.apache.catalina.core.StandardContext listenerStart
严重: Error configuring application listener of class MyServlet.OnLineCountListener
java.lang.ClassNotFoundException: MyServlet.OnLineCountListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:532)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:514)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:133)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)
2013-2-22 15:58:25 org.apache.catalina.core.StandardContext listenerStart
严重: Skipped installing application listeners due to previous error(s)
2013-2-22 15:58:25 org.apache.catalina.core.StandardContext startInternal
严重: Error listenerStart
2013-2-22 15:58:25 org.apache.catalina.core.StandardContext startInternal
严重: Context [/ServletTest] startup failed due to previous errors
2013-2-22 15:58:25 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
2013-2-22 15:58:25 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
2013-2-22 15:58:25 org.apache.catalina.startup.Catalina start
信息: Server startup in 1416 ms
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>ServletTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>MyServlet.OnLineCountListener</listener-class>
</listener>
</web-app>
package MyServlet.test;
/*public class EncodingFilter
{
}*/
// ==================== Program Discription =====================
// 程序名称:示例14-9 : OnLineCountListener .java
// 程序目的:学习使用监听器
// ==============================================================
import javax.servlet.http.*;
import javax.servlet.*;
public class OnLineCountListener implements HttpSessionListener,
ServletContextListener, ServletContextAttributeListener
{
private intcount;
private ServletContextcontext= null;
public OnLineCountListener()
{
count = 0;
// setContext();
}
// 创建一个session时激发
public void sessionCreated(HttpSessionEvent se)
{
count++;
setContext(se);
}
// 当一个session失效时激发
public void sessionDestroyed(HttpSessionEvent se)
{
count--;
setContext(se);
}
// 设置context的属性,它将激发attributeReplaced或attributeAdded方法
public void setContext(HttpSessionEvent se)
{
se.getSession().getServletContext()
.setAttribute("onLine", new Integer(count));
}
// 增加一个新的属性时激发
public void attributeAdded(ServletContextAttributeEvent event)
{
log("attributeAdded('" + event.getName() + "', '" + event.getValue()
+ "')");
}
// 删除一个新的属性时激发
public void attributeRemoved(ServletContextAttributeEvent event)
{
log("attributeRemoved('" + event.getName() + "', '" + event.getValue()
+ "')");
}
// 属性被替代时激发
public void attributeReplaced(ServletContextAttributeEvent event)
{
log("attributeReplaced('" + event.getName() + "', '" + event.getValue()
+ "')");
}
// context删除时激发
public void contextDestroyed(ServletContextEvent event)
{
log("contextDestroyed()");
this.context = null;
}
// context初始化时激发
public void contextInitialized(ServletContextEvent event)
{
this.context = event.getServletContext();
log("contextInitialized()");
}
private void log(String message)
{
System.out.println("ContextListener: " + message);
}
}