第一种方式:
第一步,创建一个类继承TimerTask,用来执行具体任务
package com.zytk;
import java.util.Date;
import java.util.TimerTask;
class TaskBean extends TimerTask
{
public void run()
{
System.out.println(new Date()); //执行具体任务
}
}
第二步,创建一个类继承HttpServlet,以便于在servlet中调用
package com.zytk;
import java.util.Timer;
import javax.servlet.http.HttpServlet;
public class TimerCallTask extends HttpServlet
{
private static final long serialVersionUID = 1L;
Timer timer;
public TimerCallTask()
{
timer = new Timer("TimerCallTask ", true);
timer.schedule(new TaskBean(), 1000, 60000);
//第一个参数,是 TimerTask 类,在包:import java.util.TimerTask .使用者要继承该类,并实现 public void run() 方法,因为 TimerTask 类 实现了 Runnable 接口。
//第二个参数的意思是,当你调用该方法后,该方法必然会调用 TimerTask 类 TimerTask 类 中的 run() 方法,这个参数就是这两者之间的差值,转换成汉语的意思就是说,用户调用 schedule() 方法后,要等待这么长的时间才可以第一次执行 run() 方法。
//第三个参数的意思就是,第一次调用之后,从第二次开始每隔多长的时间调用一次 run() 方法。
}
}
第三步,在servlet的初始化事件中调用
package com.zytk;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
public class servletTimeListener extends HttpServlet
{
TimerCallTask impDt=null;
private static final long serialVersionUID = 1L;
public void destroy()
{
super.destroy(); // Just puts "destroy" string in log
// Put your code here
//System.out.println("Stopping...");
impDt.destroy();
//System.out.println("Stopped.");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost( request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
/*response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();*/
}
public void init() throws ServletException
{
//System.out.println("Starting...");
impDt=new TimerCallTask();//这里创建一个新的任务对象,间接执行调用具体任务执行类
//System.out.println("Started Successfully.");
}
}
第四步,配置web.xml,服务器启动时候,自动加servlet:servletTimeListener
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>定时打印时间测试</description>
<display-name>启动时加载</display-name>
<servlet-name>servletTimeListener</servlet-name>
<servlet-class>com.zytk.servletTimeListener</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>