zoukankan      html  css  js  c++  java
  • tomcat启动时自动运行代码

    原文链接: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,代码如下:

    [java] view plaincopy
     
    1. package org.cai;  
    2.   
    3. import javax.servlet.ServletException;  
    4. import javax.servlet.http.HttpServlet;  
    5.   
    6. public class MyServlet extends HttpServlet {  
    7.   
    8.     @Override  
    9.     public void init() throws ServletException {  
    10.         // TODO Auto-generated method stub  
    11.         super.init();  
    12.         System.out.println("自动加载启动.");  
    13.         System.out.println("自动加载启动.");  
    14.         for(int i = 0; i < 10; i++){  
    15.               
    16.             System.out.println(i + "、 自动加载启动.");  
    17.         }  
    18.     }  
    19.   
    20.       
    21. }  

    同时,在web.xml配置文件内容如下:

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <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">  
    3.   <display-name>myweb1</display-name>  
    4.   <welcome-file-list>  
    5.     <welcome-file>index.jsp</welcome-file>  
    6.   </welcome-file-list>  
    7.   <servlet>  
    8.    <servlet-name>MyServlet</servlet-name>  
    9.    <servlet-class>org.cai.MyServlet</servlet-class>  
    10.    <strong><load-on-startup>0</load-on-startup></strong>  
    11.   </servlet>  
    12.   <servlet-mapping>  
    13.    <servlet-name>MyServlet</servlet-name>  
    14.    <url-pattern>/</url-pattern>  
    15.   </servlet-mapping>  
    16. </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类如下:

    [java] view plaincopy
     
    1. package org.cai;  
    2.   
    3. import javax.servlet.ServletContextEvent;  
    4. import javax.servlet.ServletContextListener;  
    5.   
    6. public class MyListener implements ServletContextListener {  
    7.   
    8.     private java.util.Timer timer = null ;  
    9.     public void contextDestroyed(ServletContextEvent event) {  
    10.         // TODO Auto-generated method stub  
    11.   
    12.     }  
    13.   
    14.     public void contextInitialized(ServletContextEvent event) {  
    15.   
    16.         timer = new java.util.Timer(true) ;  
    17.         event.getServletContext().log("定时器已启动。") ;  
    18.         timer.schedule(new MyTask(event.getServletContext()), 0, 5000) ;  
    19.         event.getServletContext().log("已经添加任务调度表。" ) ;  
    20.           
    21.     }  
    22.   
    23. }  


     


    contextInitialized函数里的内容将被自动执行
    最后在web.xml里面添加一个监听节点就行了
      <listener>
       <listener-class>org.cai.MyListener</listener-class>
      </listener>

    完整web.xml如下

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <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">  
    3.   <display-name>myweb2</display-name>  
    4.   <welcome-file-list>  
    5.     <welcome-file>index.jsp</welcome-file>  
    6.   </welcome-file-list>  
    7.   <listener>  
    8.     <listener-class>org.cai.MyListener</listener-class>  
    9.   </listener>  
    10. </web-app>  


     

    如果要执行定时任务,就在自动执行的代码里面使用 java.util.Timer以及创建一个继承java.util.TimerTask的类。示例:

    [html] view plaincopy
     
    1. package org.cai;  
    2.   
    3. import java.util.TimerTask;  
    4.   
    5. import javax.servlet.ServletContext;  
    6.   
    7. public class MyTask extends TimerTask {  
    8.   
    9.      private static boolean isRunning = false;   
    10.      private ServletContext context = null;   
    11.   
    12.      public MyTask(ServletContext context){  
    13.            
    14.          this.context = context ;  
    15.      }  
    16.     @Override  
    17.     public void run() {  
    18.   
    19.         if (! isRunning){  
    20.             System.out.println("开始执行指定任务.") ;  
    21.             //if (C_SCHEDULE_HOUR == c.get(Calendar.HOUR_OF_DAY)) {  
    22.                 isRunning = true ;  
    23.                 context.log("开始执行指定任务.") ;  
    24.                 //TODO 添加自定义的详细任务,以下只是示例   
    25.                 int i = 0;   
    26.                 while (i++ < 10) {   
    27.                     context.log("已完成任务的" + i + "/" + 10);   
    28.                     //System.out.println("已完成任务的" + i + "/" + 1000) ;  
    29.                 }   
    30.           
    31.                 isRunning = false;   
    32.                 context.log("指定任务执行结束");   
    33.                 System.out.println("指定任务执行结束") ;  
    34.   
    35.             //}  
    36.         }else{  
    37.               
    38.             context.log("上一次任务执行还未结束");   
    39.   
    40.         }  
    41.     }  
    42.   
    43. }  


    以上两种方法部署到tomcat,运行tomcatinstartup.bat,即可看到效果,其中方法2中的日志写入到tomcatlogslocalhost.xxxx-xx-xx.log文件中。

    标签: Tomcat
    来源:http://www.cnblogs.com/shipeng22022/p/4613990.html
  • 相关阅读:
    PHP保留小数的相关方法
    ASP.NET Core MVC 之过滤器(Filter)
    ASP.NET Core MVC 之控制器(Controller)
    ASP.NET Core MVC 之视图组件(View Component)
    ASP.NET Core MVC 之局部视图(Partial Views)
    标签助手(TagHelper)
    ASP.NET Core MVC 之布局(Layout)
    ASP.NET Core MVC 之视图(Views)
    ASP.NET Core MVC 之模型(Model)
    九卷读书:淘宝从小到大的发展 -重读《淘宝技术这十年》
  • 原文地址:https://www.cnblogs.com/ieayoio/p/5493193.html
Copyright © 2011-2022 走看看