我到网上查到下面这个代码:
修改Web.xml文件
设置每天16:30和20:00运行的Servlet
<servlet name= 'alarm ' servlet-class= 'test.TestAlarm '>
<run-at> 16:30,20:00 </run-at>
</servlet>
但是我在eclipes里修改web.xml时候 这段代码好像编译部过去!
请高手们指点一下! 该怎么能实现这个功能! 最好能带上代码!
------解决方案--------------------
1。先写一个ServletContextListener:
package test;
public class ContextListener implements ServletContextListener {
private java.util.Timer timer = null;
public void contextInitialized(ServletContextEvent event) {
timer = new java.util.Timer(true);
event.getServletContext().log( "定时器已启动 ");
timer.schedule(new MyTask(event.getServletContext()), 16:30);//每20秒(即10*1000毫秒)被触发一次,中间参数0表示无延迟
event.getServletContext().log( "已经添加任务调度表 ");
}
public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
event.getServletContext().log( "定时器销毁 ");
}
}
2.在web.xml里注册它:
<listener>
<listener-class> test.ContextListener </listener-class>
</listener>
3.za