Tomcat automatically loads a class at startup

Source: Internet
Author: User

Sometimes, when developing a web application, you need to automatically load a user's class after Tomcat starts and execute some initialization methods, such as loading the business dictionary from the database to the memory, therefore, you need to automatically load a class or run a class method at Tomcat startup. You can add a listener (servletcontextlistener configuration item) in the WEB-INF/Web. XML as follows:
1) added a listener myservletcontextlistener. Java to implement the javax. servlet. servletcontextlistener interface.
Package test. startup;

Import javax. servlet. servletcontextevent;
Import javax. servlet. servletcontextlistener;

Public class myservletcontextlistener implements servletcontextlistener
{

Public void contextdestroyed (servletcontextevent arg0)
{

}

Public void contextinitialized (servletcontextevent arg0)
{
System. Out. println ("load business Dictionary ");
Try
{
Com. EOS. Common. dict. dictmap. loaddata ();
}
Catch (exception E)
{
System. Out. println ("failed to load business dictionary! ");
E. printstacktrace ();
}
}
}

2) configure the listener web. xml:

<Listener>
<Listener-class> test. startup. myservletcontextlistener </listener-class>
</Listener>

Note: The Application Event listener is a notification class when the servlet environment or session object is created or modified. These are new content in servlet Specification Version 2.3. Registering a listener involves placing a listener element in the Web-app element of Web. xml. Although the structure of the listener element is simple, do not forget that the sequence of child elements in the Web-app element must be correctly given. The listener element is located before all servlet elements and after all filter-mapping elements. In addition, because the application lifetime listener is a new content in Serlvet Standard Version 2.3, you must use Version 2.3 or later of the web. xml dtd instead of Version 2.2, as shown below:
<Web-app id = "webapp_id" version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
...
</Web-app>

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.