简体   繁体   中英

WEB-INF path for files

I have web application with configuration files in \\WEB-INF\\etc\\config . This folder contains a few property files and one xml. I need to set up path to xml in one property file. After setuping this file is using to create object during start of service, this object reads properties from file. So, this object has to know path to the all files that was described in property file. How can I describe correct path in property file, if property file and xml file in the same dir?

Thanks.

The normal practice is to put those files in the runtime classpath or to add its root path to the runtime classpath. Then you'll be able to obtain the resource by ClassLoader#getResource() or as an InputStream by ClassLoader#getResourceAsStream() .

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("filename.xml");

All you need to specify in the properties file would then be the full qualified name (the classpath location) of the XML file.

If you really insist in fiddling with disk file system paths like that, then you need to specify paths relative from the web content (the folder wherein /WEB-INF is located) and then use ServletContext#getResource() or ServletContext#getResourceAsStream() to obtain the resource. This however adds a ServletContext dependency on your code utilizing the XML file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM