1、在web.xml中加入,注意
<servlet>
<servlet-name>TimerServlet</servlet-name>
<servlet-class>TimerServlet</servlet-class>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>TimerServlet</servlet-name>
<url-pattern>/TimerServlet</url-pattern>
</servlet-mapping>
2、编写TimerServlet类,
import java.util.Date;
import java.util.Timer;
public class TimerServlet
{
static Timer timer = null;
static
{
timer = new Timer();
Task task = new Task();
Date date = new Date();
timer.schedule(task, date, 1000);
}
}
3、编写Task类,
import java.util.TimerTask;
public class Task extends TimerTask
{
public void run()
{
System.out.println("定时任务执行");
}
}