Servlet的监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是随web应用的启动而启动,只初始化一次,随web应用的停止而销毁。主要作用是:做一些初始化的内容添加工作、设置一些基本的内容、比如一些参数或者是一些固定的对象等等。首先来看一下ServletContextListener接口的源代码:
-
public
abstract
interface ServletContextListener extends EventListener{
-
public abstract void contextInitialized(ServletContextEvent paramServletContextEvent);
-
public abstract void contextDestroyed(ServletContextEvent paramServletContextEvent);
-
}
下面利用监听器对数据库连接池DataSource的初始化演示它的使用:ListenerTest.java
-
import javax.servlet.ServletContext;
-
import javax.servlet.ServletContextEvent;
-
import javax.servlet.ServletContextListener;
-
import org.apache.commons.dbcp.BasicDataSource;
-
/**
-
* 现在来说说Servlet的监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的
-
* 服务器端程序,它也是随web应用的启动而启动,只初始化一次,随web应用的停止而销毁。主要作用是:做一些初始化
-
* 的内容添加工作、设置一些基本的内容、比如一些参数或者是一些固定的对象等等。
-
*
-
* 示例代码:使用监听器对数据库连接池DataSource进行初始化
-
*/
-
public
class ListenerTest implements ServletContextListener{
-
// 应用监听器的销毁方法
-
public void contextDestroyed(ServletContextEvent servletContextEvent) {
-
ServletContext servletContext = servletContextEvent.getServletContext();
-
// 在整个web应用销毁之前调用,将所有应用空间所设置的内容清空
-
servletContext.removeAttribute(
"dataSource");
-
System.out.println(
"销毁工作完成...");
-
}
-
// 应用监听器的初始化方法
-
public void contextInitialized(ServletContextEvent servletContextEvent) {
-
// 通过这个事件可以获取整个应用的空间
-
// 在整个web应用下面启动的时候做一些初始化的内容添加工作
-
ServletContext servletContext = servletContextEvent.getServletContext();
-
// 设置一些基本的内容;比如一些参数或者是一些固定的对象
-
// 创建DataSource对象,连接池技术 dbcp
-
BasicDataSource basicDataSource =
new BasicDataSource();
-
basicDataSource.setDriverClassName(
"com.jdbc.Driver");
-
basicDataSource.setUrl(
"jdbc:mysqlocalhost:3306/");
-
basicDataSource.setUsername(
"root");
-
basicDataSource.setPassword(
"root");
-
basicDataSource.setMaxActive(
10);
//最大连接数
-
basicDataSource.setMaxIdle(
5);
//最大管理数
-
//bds.setMaxWait(maxWait); 最大等待时间
-
// 把 DataSource 放入ServletContext空间中,
-
// 供整个web应用的使用(获取数据库连接)
-
servletContext.setAttribute(
"dataSource", basicDataSource);
-
System.out.println(
"应用监听器初始化工作完成...");
-
System.out.println(
"已经创建DataSource...");
-
}
-
}
web.xml中配置如下,很简单:
-
<!-- 配置应用监听器 -->
-
<listener>
-
<listener-class>com.ycq.ListenerTest
</listener-class>
-
</listener>
这样配置好了之后,以后在web应用中就可以通过ServletContext取得BasicDataSource对象,从而获取与数据库的连接,提高性能,方便使用。
示例代码二:
-
import java.io.File;
-
import javax.servlet.ServletContextEvent;
-
import javax.servlet.ServletContextListener;
-
import com.i2f.fsp.deploy.TransactionDeployer;
-
/**
-
* 监听器随着项目的启动而启动
-
*
-
*/
-
public
class ListenerTest2 implements ServletContextListener{
-
// 销毁监听器
-
public void contextDestroyed(ServletContextEvent servletContextEvent) {
-
System.out.println(
"date20161020095500 :" + servletContextEvent.getServletContext());
-
}
-
public void contextInitialized(ServletContextEvent servletContextEvent) {
-
try{
-
// 获取项目跟路径
-
String basePath = servletContextEvent.getServletContext().getRealPath(
"/");
-
// D:\apache-tomcat-6.0.41\webapps\i2money\ 绝对路径
-
System.out.println(
"basePath20161020094700 :" + basePath);
-
if (!(basePath.endsWith(File.separator))){
-
basePath = basePath + File.separator;
-
}
-
basePath = basePath +
"WEB-INF" + File.separator +
"classes" + File.separator;
-
new TransactionDeployer(basePath).deploy();
-
// D:\apache-tomcat-6.0.41\webapps\i2money\WEB-INF\classes\
-
System.out.println(
"basePath20161020094701 :" + basePath);
-
}
-
catch (Exception e){
-
e.printStackTrace();
-
System.exit(-
1);
-
}
-
}
-
}
示例代码三:
-
import javax.servlet.http.HttpSession;
-
import javax.servlet.http.HttpSessionEvent;
-
import javax.servlet.http.HttpSessionListener;
-
import org.apache.commons.logging.Log;
-
import org.apache.commons.logging.LogFactory;
-
import org.springframework.context.ApplicationContext;
-
import org.springframework.web.context.support.WebApplicationContextUtils;
-
public
class UserLogoutListener implements HttpSessionListener{
-
protected
final Log log = LogFactory.getLog(
super.getClass());
-
public void sessionCreated(HttpSessionEvent event){
-
this.log.error(
"session created. id = " + event.getSession().getId());
-
}
-
public void sessionDestroyed(HttpSessionEvent event){
-
this.log.error(
"session destroyed.id = " + event.getSession().getId());
-
HttpSession session = event.getSession();
-
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
-
OnlineUserMonitorClient client = (OnlineUserMonitorClient)context.getBean(
"onlineUserMonitorClient");
-
client.afterSessionDestroyed(session);
-
}
-
}
监听器在实际项目中的应用,监听器在java web中应用的较多,比如:统计当前在线人数、自定义session扫描器。
--------------------- 应用一:统计当前在线人数 ---------------------
-
import javax.servlet.ServletContext;
-
import javax.servlet.http.HttpSessionEvent;
-
import javax.servlet.http.HttpSessionListener;
-
/**
-
* @description HttpSessionListener监听器实现统计网站在线人数的功能
-
*/
-
public
class SessionListener implements HttpSessionListener{
-
-
public
static
int TOTAL_ONLINE_USERS =
0;
-
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
-
ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
-
TOTAL_ONLINE_USERS = (Integer) servletContext.getAttribute(
"TOTAL_ONLINE_USERS");
-
// 如果用户退出,TOTAL_ONLINE_USERS自减1
-
if(TOTAL_ONLINE_USERS ==
0){
-
servletContext.setAttribute(
"TOTAL_ONLINE_USERS",
1);
-
}
-
else{
-
TOTAL_ONLINE_USERS--;
-
servletContext.setAttribute(
"TOTAL_ONLINE_USERS", TOTAL_ONLINE_USERS);
-
}
-
}
-
-
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
-
ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
-
TOTAL_ONLINE_USERS = (Integer) servletContext.getAttribute(
"TOTAL_ONLINE_USERS");
-
// 如果用户登录,TOTAL_ONLINE_USERS自增1
-
if(TOTAL_ONLINE_USERS ==
0){
-
servletContext.setAttribute(
"TOTAL_ONLINE_USERS",
1);
-
}
-
else{
-
TOTAL_ONLINE_USERS++;
-
servletContext.setAttribute(
"TOTAL_ONLINE_USERS", TOTAL_ONLINE_USERS);
-
}
-
}
-
}
--------------------- 应用二:自定义session扫描器 ---------------------
-
import java.util.LinkedList;
-
import java.util.List;
-
import java.util.Timer;
-
import javax.servlet.ServletContextEvent;
-
import javax.servlet.ServletContextListener;
-
import javax.servlet.http.HttpSession;
-
import javax.servlet.http.HttpSessionEvent;
-
import javax.servlet.http.HttpSessionListener;
-
import jeus.util.concurrent50.Collections;
-
/**
-
* @description 当网站用户量增加时,session占用的内存会越来越大,这时session的管理,将会是一项很大的
-
* 系统开销,为了高效的管理session,我们可以写一个监听器,定期清理掉过期的session
-
*/
-
public
class SessionScanerListener implements HttpSessionListener,ServletContextListener{
-
// 创建一个线程安全的集合,用来存储session
-
@SuppressWarnings(
"unchecked")
-
List<HttpSession> sessionList = Collections.synchronizedList(
new LinkedList<HttpSession>());
-
private Object lock =
new Object();
-
-
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
-
System.out.println(
"session 创建成功...");
-
HttpSession httpSession = httpSessionEvent.getSession();
-
synchronized (lock){
-
sessionList.add(httpSession);
-
}
-
}
-
-
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
-
System.out.println(
"session 销毁成功...");
-
}
-
// web应用关闭时触发contextDestroyed事件
-
public void contextDestroyed(ServletContextEvent servletContextEvent) {
-
System.out.println(
"web应用关闭...");
-
}
-
-
// web应用启动时触发contextInitialized事件
-
public void contextInitialized(ServletContextEvent servletContextEvent) {
-
System.out.println(
"web应用初始化...");
-
// 创建定时器
-
Timer timer =
new Timer();
-
// 每隔30秒就定时执行任务
-
timer.schedule(
new MyTask(sessionList,lock),
0,
1000*
30);
-
}
-
}
-
import java.util.List;
-
import java.util.ListIterator;
-
import java.util.TimerTask;
-
import javax.servlet.http.HttpSession;
-
/**
-
* 定时器,定义定时任务的具体内容
-
*/
-
public
class MyTask extends TimerTask{
-
private List<HttpSession> list;
-
// 存储传递过来的锁
-
private Object lock;
-
// 构造方法
-
MyTask(List<HttpSession> list, Object lock){
-
this.list = list;
-
this.lock = lock;
-
}
-
@Override
-
public void run() {
-
// 考虑到多线程的情况,这里必须要同步
-
synchronized (lock){
-
System.out.println(
"定时器开始执行...");
-
ListIterator<HttpSession> listIterator = list.listIterator();
-
while(listIterator.hasNext()){
-
HttpSession httpSession = listIterator.next();
-
// httpSession.getLastAccessedTime() = session的最后访问时间
-
if(System.currentTimeMillis() - httpSession.getLastAccessedTime() >
1000*
30){
-
// 手动销毁session
-
httpSession.invalidate();
-
// 从集合中移除已经被销毁的session
-
listIterator.remove();
-
}
-
}
-
}
-
}
-
}