jsp/servlet定时监控的两种实现方式

第一种方式:

第一步,创建一个类继承TimerTask,用来执行具体任务

[java]  view plain  copy
  1. package com.zytk;  
  2.   
  3. import java.util.Date;  
  4. import java.util.TimerTask;  
  5.   
  6. class   TaskBean   extends   TimerTask  
  7. {   
  8.     public   void   run()  
  9.     {   
  10.     System.out.println(new   Date()); //执行具体任务  
  11.     }   
  12. }   

第二步,创建一个类继承HttpServlet,以便于在servlet中调用

[java]  view plain  copy
  1. package com.zytk;  
  2.   
  3. import java.util.Timer;  
  4. import javax.servlet.http.HttpServlet;  
  5.   
  6. public class TimerCallTask extends HttpServlet    
  7. {  
  8.     private static final long serialVersionUID = 1L;  
  9.     Timer timer;  
  10.   
  11.     public TimerCallTask()  
  12.     {  
  13.     timer = new Timer("TimerCallTask "true);  
  14.     timer.schedule(new TaskBean(), 100060000);  
  15.     //第一个参数,是 TimerTask 类,在包:import java.util.TimerTask .使用者要继承该类,并实现 public void run() 方法,因为 TimerTask 类 实现了 Runnable 接口。  
  16.     //第二个参数的意思是,当你调用该方法后,该方法必然会调用 TimerTask 类 TimerTask 类 中的 run() 方法,这个参数就是这两者之间的差值,转换成汉语的意思就是说,用户调用 schedule() 方法后,要等待这么长的时间才可以第一次执行 run() 方法。  
  17.     //第三个参数的意思就是,第一次调用之后,从第二次开始每隔多长的时间调用一次 run() 方法。  
  18.     }  
  19.       
  20. }  


第三步,在servlet的初始化事件中调用

[java]  view plain  copy
  1. package com.zytk;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10. import javax.servlet.ServletContext;  
  11.   
  12. public class servletTimeListener extends HttpServlet  
  13. {  
  14.     TimerCallTask impDt=null;  
  15.     private static final long serialVersionUID = 1L;  
  16.   
  17.     public void destroy()  
  18.     {  
  19.     super.destroy(); // Just puts "destroy" string in log  
  20.     // Put your code here  
  21.     //System.out.println("Stopping...");  
  22.     impDt.destroy();  
  23.     //System.out.println("Stopped.");  
  24.     }  
  25.   
  26.      
  27.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  28.         throws ServletException, IOException  
  29.     {  
  30.     doPost( request,  response);  
  31.       
  32.     }  
  33.   
  34.       
  35.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  36.         throws ServletException, IOException  
  37.     {  
  38.   
  39.     /*response.setContentType("text/html"); 
  40.     PrintWriter out = response.getWriter(); 
  41.     out 
  42.         .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 
  43.     out.println("<HTML>"); 
  44.     out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 
  45.     out.println("  <BODY>"); 
  46.     out.print("    This is "); 
  47.     out.print(this.getClass()); 
  48.     out.println(", using the POST method"); 
  49.     out.println("  </BODY>"); 
  50.     out.println("</HTML>"); 
  51.     out.flush(); 
  52.     out.close();*/  
  53.     }  
  54.   
  55.     public void init() throws ServletException  
  56.     {  
  57.     //System.out.println("Starting...");  
  58.     impDt=new TimerCallTask();//这里创建一个新的任务对象,间接执行调用具体任务执行类  
  59.     //System.out.println("Started Successfully.");  
  60.     }  
  61.   
  62. }  

第四步,配置web.xml,服务器启动时候,自动加servlet:servletTimeListener

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.  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">  
  4.  <servlet>  
  5.   <description>定时打印时间测试</description>  
  6.   <display-name>启动时加载</display-name>  
  7.   <servlet-name>servletTimeListener</servlet-name>  
  8.   <servlet-class>com.zytk.servletTimeListener</servlet-class>  
  9.   <load-on-startup>0</load-on-startup>  
  10.  </servlet>  
  11.   
  12.    
  13.  <welcome-file-list>  
  14.   <welcome-file>index.jsp</welcome-file>  
  15.  </welcome-file-list>  
  16.  <login-config>  
  17.   <auth-method>BASIC</auth-method>  
  18.  </login-config>  
  19. </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


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值