原文链接:http://jingpin.jikexueyuan.com/article/49660.html
作者: 一直向北发布时间:2015-07-13 11:12:13方法1:tomcat 自动执行servlet
写一个servlet,在init()方法中写好想要执行的程序,程序如下:
eclipse新建一个webDynamic Web Project , name = myweb1 , 把tomcat中的 servlet-api.jar复制到lib中,新建一个类MyServlet.java, 继承 javax.servlet.HttpServlet,代码如下:
- package org.cai;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- public class MyServlet extends HttpServlet {
- @Override
- public void init() throws ServletException {
- // TODO Auto-generated method stub
- super.init();
- System.out.println("自动加载启动.");
- System.out.println("自动加载启动.");
- for(int i = 0; i < 10; i++){
- System.out.println(i + "、 自动加载启动.");
- }
- }
- }
同时,在web.xml配置文件内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
- <display-name>myweb1</display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>MyServlet</servlet-name>
- <servlet-class>org.cai.MyServlet</servlet-class>
- <strong><load-on-startup>0</load-on-startup></strong>
- </servlet>
- <servlet-mapping>
- <servlet-name>MyServlet</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
其中<load-on-startup>0</load-on-startup>即可实现tomcat启动时执行该servlet
load-on-startup的值必须为整数
当load-on-startup值为负整数或者未指定时,容器在该servlet被调用时加载
当其值为0和正整数时,容器启动时加载,值越小,优先级越高
使用虚拟路径部署到tomcat中:在 tomcat的 server.xml的 </Host>前加入:
<Context debug="0" docBase="F:/studyworkspace/myweb1/WebRoot" path="myweb1" reloadable="true">
</Context>docBase为 myweb1的webroot的路径 ,path为上下文
方法2:Servlet侦听器要运用Servlet侦听器需要实现javax.servlet.ServletContextListener接口,同时实现它的contextInitialized(ServletContextEvent event)和contextDestroyed(ServletContextEvent event)两个接口函数。
listener类如下:
- package org.cai;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- public class MyListener implements ServletContextListener {
- private java.util.Timer timer = null ;
- public void contextDestroyed(ServletContextEvent event) {
- // TODO Auto-generated method stub
- }
- public void contextInitialized(ServletContextEvent event) {
- timer = new java.util.Timer(true) ;
- event.getServletContext().log("定时器已启动。") ;
- timer.schedule(new MyTask(event.getServletContext()), 0, 5000) ;
- event.getServletContext().log("已经添加任务调度表。" ) ;
- }
- }
contextInitialized函数里的内容将被自动执行
最后在web.xml里面添加一个监听节点就行了
<listener>
<listener-class>org.cai.MyListener</listener-class>
</listener>完整web.xml如下
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
- <display-name>myweb2</display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <listener>
- <listener-class>org.cai.MyListener</listener-class>
- </listener>
- </web-app>
如果要执行定时任务,就在自动执行的代码里面使用 java.util.Timer以及创建一个继承java.util.TimerTask的类。示例:
- package org.cai;
- import java.util.TimerTask;
- import javax.servlet.ServletContext;
- public class MyTask extends TimerTask {
- private static boolean isRunning = false;
- private ServletContext context = null;
- public MyTask(ServletContext context){
- this.context = context ;
- }
- @Override
- public void run() {
- if (! isRunning){
- System.out.println("开始执行指定任务.") ;
- //if (C_SCHEDULE_HOUR == c.get(Calendar.HOUR_OF_DAY)) {
- isRunning = true ;
- context.log("开始执行指定任务.") ;
- //TODO 添加自定义的详细任务,以下只是示例
- int i = 0;
- while (i++ < 10) {
- context.log("已完成任务的" + i + "/" + 10);
- //System.out.println("已完成任务的" + i + "/" + 1000) ;
- }
- isRunning = false;
- context.log("指定任务执行结束");
- System.out.println("指定任务执行结束") ;
- //}
- }else{
- context.log("上一次任务执行还未结束");
- }
- }
- }
以上两种方法部署到tomcat,运行tomcatinstartup.bat,即可看到效果,其中方法2中的日志写入到tomcatlogslocalhost.xxxx-xx-xx.log文件中。标签: Tomcat来源:http://www.cnblogs.com/shipeng22022/p/4613990.html