"Recently I wanted to start my standalone Java Application on Tomcat Startup. Also found so many other related questions on net. i.e. [图片] I need to run an appli .."

How to Run Java Program Automatically on Tomcat Startup

Recently I wanted to start my standalone Java Application on Tomcat Startup. Also found so many other related questions on net. i.e.

ApacheTomcatCrunchifyTipsjpg

To run java program automatically on tomcat startup, need to use Servlet and this Servlet initialized on tomcat startup automatically.

To execute a program, you have to use Servlet and Servlet should define in deployment descriptor web.xml file under WEB-INF folder.

web.xml file contain tags and tag. Servlet tag keeps information of Servlet class. When tomcat starts, all Servlet loads in web container and init method of Servlet loaded first. Any java statement in init method of Servlet can be executed on running tomcat startup batch or shell.

In init method we can define our scripts which have to be executed e.g. sending emails, sending newsletters, starting scheduler, etc…

Below is a simple trick to run your java program automatically on Tomcat Startup.

Step-1

Modify Web.xml file with below information. Where CrunchifyServletExample is a class name and crunchify.com.tutorials is a package name.

Modify these values as per your need.

<servlet>

    <servlet-name>CrunchifyTutorials</servlet-name>
    <servlet-class>crunchify.com.tutorials.CrunchifyServletExample</servlet-class>
    <load-on-startup>1</load-on-startup>

</servlet>

This is my complete web.xml file

<?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_3_0.xsd"  version="3.0">

  <display-name>CrunchifyTutorials</display-name>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>CrunchifyTutorials</servlet-name>
    <servlet-class>crunchify.com.tutorials.CrunchifyServletExample</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

</web-app>

Step-2 CrunchifyServletExample.java

package  crunchify.com.tutorials;

import  javax.servlet.*;
import  javax.servlet.http.HttpServlet;

/**
* @author Crunchify.com
*/
@SuppressWarnings("serial")
public  class  CrunchifyServletExample  extends  HttpServlet{

    public  void  init()  throws  ServletException{
          System.out.println("----------");
          System.out.println("---------- CrunchifyServletExample Initialized successfully ----------");
          System.out.println("----------");
    }
}

Step-3

Now Clean your project using Maven or Project Menu -> Clean

Step-4

Console Output

----------
----------  CrunchifyServletExample Initialized successfully  ----------
----------

Enjoy and Happy Coding…

感谢原作者,博客转自:https://crunchify.com/how-to-run-java-program-automatically-on-tomcat-startup

  • B3log

    B3log 是一个开源组织,名字来源于“Bulletin Board Blog”缩写,目标是将独立博客与论坛结合,形成一种新的网络社区体验,详细请看 B3log 构思。目前 B3log 已经开源了多款产品:PipeSoloSymWide 等,欢迎大家加入,贡献开源。

    2594 引用 • 4225 回帖 • 632 关注
  • 部署
    18 引用 • 285 回帖
  • Tomcat

    Tomcat 最早是由 Sun Microsystems 开发的一个 Servlet 容器,在 1999 年被捐献给 ASF(Apache Software Foundation),隶属于 Jakarta 项目,现在已经独立为一个顶级项目。Tomcat 主要实现了 JavaEE 中的 Servlet、JSP 规范,同时也提供 HTTP 服务,是市场上非常流行的 Java Web 容器。

    112 引用 • 445 回帖
  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    2154 引用 • 7283 回帖 • 1050 关注
感谢    关注    收藏    赞同    反对    举报    分享
回帖    
请输入回帖内容...