First create a class
public class Contextinitlistener implements Servletcontextlistener
Makes the Class A listener. Used to listen to the entire container life cycle, primarily initialized and destroyed.
After the class is created, add a simple listener configuration to the Web. XML configuration file, which is the class that we just defined.
XML code
<Listener> <!--Lang:xml - <Description>Servletcontextlistener</Description> <!--Lang:xml - <Listener-class>Com.test.web.filter.ContextInitListener</Listener-class> <!--Lang:xml -</Listener>
After configuring the listener, we begin to write Contextinitlistener code. The implementation of the interface will automatically generate two methods, initialization and destruction, we just paste this bar, another useless. Web projects Generally, the relative path is generally in web-inf/classes, get the file under the path, preferably with GetClass (). getResourceAsStream ("/baseconfig.properties");
Java code
@Override public void Contextinitialized (Servletcontextevent sce) {Properties props = new Properties (); InputStream InputStream = null try {InputStream = GetClass (). getResourceAsStream ("/baseconfig.properties" ); Props.load (InputStream); String TempPath = (string) props.get ("path" ); catch (IOException ex) {Ex.print StackTrace (); } }
Transferred from: http://my.oschina.net/u/585275/blog/109227
Automatically load custom properties files when Java Web project starts