第一种方式:
第一步,创建一个类继承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>
第二种方法
第一步,首先创建一个具体执行任务的类,继承TimerTask
package com.zytk;
import java.util.Date;
import java.util.TimerTask;
public class theTask extends TimerTask
{
@Override
public void run() //重载run方法
{
System.out.println(new Date()); //执行具体任务
}
}
第二步,创建一个Servlet容器监听类,调用上面的类执行定时任务
package com.zytk;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class TaskListener implements ServletContextListener
{
private Timer timer = null;
//@Override
public void contextInitialized(ServletContextEvent event)
{
event.getServletContext().log("starting log...");
System.out.println("Starting...");
timer = new Timer(true);
timer.schedule(new theTask(), 0, 10* 1000);
}
//@Override
public void contextDestroyed(ServletContextEvent event)
{
System.out.println("stopping...");
timer.cancel();
event.getServletContext().log("stopping log...");
System.out.println("stopping successfully.");
}
}
第三步,配置web.xml文件中的对应监听类内容:
<?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">
<listener>
<listener-class>com.zytk.TaskListener</listener-class>
</listener>
</web-app>
转载自:https://blog.csdn.net/CHUANGHUI/article/details/6991835
https://blog.csdn.net/chuanghui/article/details/6991861