How to run a Java class when Tomcat is started

Source: Internet
Author: User

Just set up a listener and specify a description in Web. Xml.

Web. XML is actually a description file that will be read when Tomcat starts, such as when accessing the server home page can be specified in the inside, there is a corresponding tag. Here's an explanation: http://blog.chinaunix.net/uid-20399471-id-1687965.html

To implement this listener interface:

1  Public classMyservletcontextlistenerImplementsServletcontextlistener {2 3 @Override4    Public voidcontextdestroyed (servletcontextevent arg0) {5     //Notification that, the servlet context is on to being shut down. 6   }7 8 @Override9    Public voidcontextinitialized (servletcontextevent arg0) {Ten     //Do all the tasks, the need to perform just after the server starts One  A     //Notification The Web application initialization process is starting -   } -  the}

Then write the listener tag in Web. XML and add the Listener class:

Mytestlistener replace the class name that implements the Servletcontextlistener interface yourself
< Listener >     < Listener-class > [Package]. Mytestlistener</listener-class>    </listener  >  

References

Http://stackoverflow.com/questions/13819773/how-to-run-specific-java-code-on-tomcat-start-or-on-application-deploy

Http://stackoverflow.com/questions/158336/is-there-a-way-to-run-a-method-class-only-on-tomcat-startup

http://crunchify.com/how-to-run-java-program-automatically-on-tomcat-startup/

Http://blog.sina.com.cn/s/blog_6ea90e4b0100ny3t.html

How to run a Java class when Tomcat is started

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.