第一種方式:
第一步,創建一個類繼承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>