1. 程式人生 > >tomcat啟動後讓servlet一直執行

tomcat啟動後讓servlet一直執行

.1.web.xml 中設定servlet為<load-on-startup>1</load-on-startup>

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appversion="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方法中執行啟動業務執行緒

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

3.AutoRunThread 定時執行

  1. /** 
  2.  *  
  3.  */
  4. /** 
  5.  * @author baijd 
  6.  *  
  7.  */
  8. publicclass AutoRunThread extends Thread  
  9. {  
  10.     public AutoRunThread()  
  11.     {  
  12.         super();  
  13.         // TODO Auto-generated constructor stub
  14.     }  
  15.     publicvoid run()  
  16.     {  
  17.         try
  18.         {  
  19.             do
  20.             {  
  21.                 // do something
  22.                 System.out.println("定時執行....");