tomcat啟動後讓servlet一直執行
阿新 • • 發佈:2019-01-18
.1.web.xml 中設定servlet為<load-on-startup>1</load-on-startup>
- <?xmlversion="1.0"encoding="UTF-8"?>
- <web-appversion="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>This is the description of my J2EE component</description>
- <display-name>This is the display name of my J2EE component</display-name>
- <servlet-name>AutoRunService</servlet-name>
-
<servlet-class>AutoRunService</servlet-class
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>AutoRunService</servlet-name>
- <url-pattern>/servlet/AutoRunService</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
-
<welcome-file>index.jsp</
- </welcome-file-list>
- <login-config>
- <auth-method>BASIC</auth-method>
- </login-config>
- </web-app>
2.AutoRunService在init方法中執行啟動業務執行緒
- 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;
- publicclass AutoRunService extends HttpServlet
- {
- /**
- *
- */
- privatestaticfinallong serialVersionUID = 1L;
- /**
- * Constructor of the object.
- */
- public AutoRunService()
- {
- super();
- }
- /**
- * Destruction of the servlet. <br>
- */
- publicvoid destroy()
- {
- super.destroy(); // Just puts "destroy" string in log
- // Put your code here
- }
- /**
- * The doGet method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to get.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- publicvoid doGet(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 GET method");
- out.println(" </BODY>");
- out.println("</HTML>");
- out.flush();
- out.close();
- }
- /**
- * The doPost method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to post.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- publicvoid 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();
- }
- /**
- * Initialization of the servlet. <br>
- *
- * @throws ServletException if an error occurs
- */
- publicvoid init() throws ServletException
- {
- // Put your code here
- AutoRunThread autoRunThread = new AutoRunThread();
- autoRunThread.start();
- }
- }
3.AutoRunThread 定時執行
- /**
- *
- */
- /**
- * @author baijd
- *
- */
- publicclass AutoRunThread extends Thread
- {
- public AutoRunThread()
- {
- super();
- // TODO Auto-generated constructor stub
- }
- publicvoid run()
- {
- try
- {
- do
- {
- // do something
- System.out.println("定時執行....");