tomcat启动后让servlet一直运行

.1.web.xml 中设置servlet为<load-on-startup>1</load-on-startup>

[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>This is the description of my J2EE component</description>  
  6.   <display-name>This is the display name of my J2EE component</display-name>  
  7.   <servlet-name>AutoRunService</servlet-name>  
  8.   <servlet-class>AutoRunService</servlet-class>  
  9.   <load-on-startup>1</load-on-startup>  
  10.  </servlet>  
  11.  <servlet-mapping>  
  12.   <servlet-name>AutoRunService</servlet-name>  
  13.   <url-pattern>/servlet/AutoRunService</url-pattern>  
  14.  </servlet-mapping>  
  15.  <welcome-file-list>  
  16.   <welcome-file>index.jsp</welcome-file>  
  17.  </welcome-file-list>  
  18.  <login-config>  
  19.   <auth-method>BASIC</auth-method>  
  20.  </login-config>  
  21. </web-app>  


2.AutoRunService在init方法中执行启动业务线程

[java]  view plain copy
  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3.   
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9.   
  10. public class AutoRunService extends HttpServlet  
  11. {  
  12.   
  13.     /** 
  14.      *  
  15.      */  
  16.     private static final long serialVersionUID = 1L;  
  17.   
  18.     /** 
  19.      * Constructor of the object. 
  20.      */  
  21.     public AutoRunService()  
  22.     {  
  23.         super();  
  24.     }  
  25.   
  26.     /** 
  27.      * Destruction of the servlet. <br> 
  28.      */  
  29.     public void destroy()  
  30.     {  
  31.         super.destroy(); // Just puts "destroy" string in log  
  32.         // Put your code here  
  33.     }  
  34.   
  35.     /** 
  36.      * The doGet method of the servlet. <br> 
  37.      * 
  38.      * This method is called when a form has its tag value method equals to get. 
  39.      *  
  40.      * @param request the request send by the client to the server 
  41.      * @param response the response send by the server to the client 
  42.      * @throws ServletException if an error occurred 
  43.      * @throws IOException if an error occurred 
  44.      */  
  45.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  46.             throws ServletException, IOException  
  47.     {  
  48.   
  49.         response.setContentType("text/html");  
  50.         PrintWriter out = response.getWriter();  
  51.         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");  
  52.         out.println("<HTML>");  
  53.         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");  
  54.         out.println("  <BODY>");  
  55.         out.print("    This is ");  
  56.         out.print(this.getClass());  
  57.         out.println(", using the GET method");  
  58.         out.println("  </BODY>");  
  59.         out.println("</HTML>");  
  60.         out.flush();  
  61.         out.close();  
  62.     }  
  63.   
  64.     /** 
  65.      * The doPost method of the servlet. <br> 
  66.      * 
  67.      * This method is called when a form has its tag value method equals to post. 
  68.      *  
  69.      * @param request the request send by the client to the server 
  70.      * @param response the response send by the server to the client 
  71.      * @throws ServletException if an error occurred 
  72.      * @throws IOException if an error occurred 
  73.      */  
  74.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  75.             throws ServletException, IOException  
  76.     {  
  77.   
  78.         response.setContentType("text/html");  
  79.         PrintWriter out = response.getWriter();  
  80.         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");  
  81.         out.println("<HTML>");  
  82.         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");  
  83.         out.println("  <BODY>");  
  84.         out.print("    This is ");  
  85.         out.print(this.getClass());  
  86.         out.println(", using the POST method");  
  87.         out.println("  </BODY>");  
  88.         out.println("</HTML>");  
  89.         out.flush();  
  90.         out.close();  
  91.     }  
  92.   
  93.     /** 
  94.      * Initialization of the servlet. <br> 
  95.      * 
  96.      * @throws ServletException if an error occurs 
  97.      */  
  98.     public void init() throws ServletException  
  99.     {  
  100.         // Put your code here  
  101.           
  102.         AutoRunThread autoRunThread = new AutoRunThread();  
  103.         autoRunThread.start();  
  104.     }  
  105.   
  106. }  

3.AutoRunThread 定时运行

[java]  view plain copy
  1. /** 
  2.  *  
  3.  */  
  4.   
  5. /** 
  6.  * @author baijd 
  7.  *  
  8.  */  
  9. public class AutoRunThread extends Thread  
  10. {  
  11.   
  12.     public AutoRunThread()  
  13.     {  
  14.         super();  
  15.         // TODO Auto-generated constructor stub  
  16.     }  
  17.   
  18.     public void run()  
  19.     {  
  20.         try  
  21.         {  
  22.             do  
  23.             {  
  24.                 // do something  
  25.                 System.out.println("定时执行....");  
  26.   
  27.                 // sleep(60 * 1000);这儿写的是2小时  
  28.                 sleep(2  * 1000);  
  29.             } while (true);  
  30.         } catch (Exception e)  
  31.         {  
  32.         }  
  33.     }  
  34. }  

http://blog.csdn.net/baijd_ss/article/details/8489142
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值