欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

配制监听器

程序员文章站 2022-05-16 22:07:16
...

 1、配制web.xml

<!-- 监听器 ******************************************************************************* -->
<listener>
	<listener-class>com.test.InitNews</listener-class>
</listener>
<!-- 监听器结束 ============================================================================ -->

 

2、创建监听类InitNews

package com.test;

import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class InitNews implements ServletContextListener {

	
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		
	}

	public void contextInitialized(ServletContextEvent sce) {

		Timer timer = new Timer();
		
		timer.schedule(new News(sce), 1000,1*1000);		
	}

}

 

 3、具体功能类

package com.test;

import java.util.TimerTask;

import javax.servlet.ServletContextEvent;

public class News extends TimerTask {
	
	private ServletContextEvent sce;
	
	public News(ServletContextEvent sce)
	{
		this.sce = sce;
	}
	
	public void run()
	{
		System.out.println("监听器,每隔一秒钟显示一次!!");
	}

}