配制监听器
程序员文章站
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("监听器,每隔一秒钟显示一次!!");
}
}
上一篇: PHP实现上传文件并存进数据库的方法,
下一篇: 数据库设计多对多关系的几种形态