严重: 属性事件监听器引发的异常
java.lang.ClassCastException: javax.servlet.ServletContextAttributeEvent cannot be cast to javax.servlet.ServletContext
at com.listener.MyServletContextListener.attributeReplaced(MyServletContextListener.java:23)
at org.apache.catalina.core.ApplicationContext.setAttribute(ApplicationContext.java:751)
at org.apache.catalina.core.ApplicationContextFacade.setAttribute(ApplicationContextFacade.java:399)
at com.servlet.EventTestServlet.doGet(EventTestServlet.java:18)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol
C
o
n
n
e
c
t
i
o
n
H
a
n
d
l
e
r
.
p
r
o
c
e
s
s
(
A
b
s
t
r
a
c
t
P
r
o
t
o
c
o
l
.
j
a
v
a
:
834
)
a
t
o
r
g
.
a
p
a
c
h
e
.
t
o
m
c
a
t
.
u
t
i
l
.
n
e
t
.
N
i
o
E
n
d
p
o
i
n
t
ConnectionHandler.process(AbstractProtocol.java:834) at org.apache.tomcat.util.net.NioEndpoint
ConnectionHandler.process(AbstractProtocol.java:834)atorg.apache.tomcat.util.net.NioEndpointSocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor
W
o
r
k
e
r
.
r
u
n
(
T
h
r
e
a
d
P
o
o
l
E
x
e
c
u
t
o
r
.
j
a
v
a
:
624
)
a
t
o
r
g
.
a
p
a
c
h
e
.
t
o
m
c
a
t
.
u
t
i
l
.
t
h
r
e
a
d
s
.
T
a
s
k
T
h
r
e
a
d
Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread
Worker.run(ThreadPoolExecutor.java:624)atorg.apache.tomcat.util.threads.TaskThreadWrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
`package com.listener;
import java.util.Date;
import javax.servlet.*;
public final class MyServletContextListener implements ServletContextListener,ServletContextAttributeListener{
private ServletContext context = null;
public void contextInitialized(ServletContextEvent scae){
System.out.println("应用程序启动时间:" + new Date());
}
public void contextDestroyed(ServletContextEvent scae){
System.out.println("应用程序关闭时间:" + new Date());
}
public void attributeAdded(ServletContextAttributeEvent scae){
System.out.println("添加了一个上下文属性:"+scae.getName() + "="+scae.getValue());
}
public void attributeRemoved(ServletContextAttributeEvent scae){
System.out.println("删除了一个上下文属性:"+scae.getName() + "=" + scae.getValue());
}
public void attributeReplaced(ServletContextAttributeEvent scae){
System.out.print("修改了一个上下文属性,旧值: "+scae.getName() + "=" + scae.getValue());
System.out.println(" ,新值: "+scae.getName() + "=" + ((ServletContext) scae).getAttribute(scae.getName()));
}
}
`