事件类型
|
解释
|
监听器接口
|
接口方法
|
ServletContext事件
|
Web应用程序(即ServletContext)刚被创建并能够处理web请求,或者web应用程序即将被关闭
|
Javax.servlet.
ServletContextLinstener
|
contextInitialized
(ServletContextEvent event)
|
contextDestroyed
(ServletContextEvent event)
|
Package
com.ty.lintener ;
import
javax.naming.*;
import
javax.servlet.*;
import
javax.sql.DataSource;
public
class
ServletContextListenerImpl
implements
ServletContextListener {
public
ServletContextListenerImpl () {
//
TODO
Auto-generated constructor stub
}
public
void
contextDestroyed(ServletContextEvent event) {
event.getServletContext().removeAttribute(
"DataSource"
) ;
}
public
void
contextInitialized(ServletContextEvent event) {
ServletContext application = event.getServletContext() ;
String name = application.getInitParameter(
"DataSource"
) ;
if
(name !=
null
) {
try
{
InitialContext context =
new
InitialContext() ;
DataSource ds = (DataSource)context.lookup(name) ;
if
(ds !=
null
) {
application.setAttribute(
"DataSource"
, ds) ;
}
}
catch
(NamingException e) {
e.printStackTrace();
}
}
}
}
|
<
context-param
>
<
param-name
>
DataSource
</
param-name
>
<
param-value
>
java:comp/env/jdbc/bbs
</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>
com.ty.listener.
ServletContextListenerImpl
</
listener-class
>
</
listener
>
|