I need to put some settings files in the 'working directory' in order to configure my app.
I have tried puting it in ./WEB-INF/ but did not work, any idea where is the 'working directory'?, and, is it a way to get it programatically?
thanks.
I need to put some settings files in the 'working directory' in order to configure my app.
I have tried puting it in ./WEB-INF/ but did not work, any idea where is the 'working directory'?, and, is it a way to get it programatically?
thanks.
Although I realize you didn't specifically ask this question maybe this will address your real problem ...
Have you considered putting your config files under WEB-INF/classes ? Many configuration files in a web application are loaded as resources, not as "file" objects. If you put your configuration files under WEB-INF/classes they will be available as resources through the ClassLoader.
EDIT: As a side note, there's a slight danger in putting your config files directly under WEB-INF, as they may be exposed to end users by just typing in the correct URL. This depends on other settings in your web container, but it is a real possibility and has happened more than once. Hope you don't do something like keep DB connection info there ...
The Servlet specification provides your web application with a special ServletContext-specific working directory which has read, write and delete permissions according to the standard security policy of Tomcat:
File workingDir = (File)servletContext.getAttribute(ServletContext.TEMPDIR);
You will normally create a subdirectory under it for your specific needs since other components, in particular Tomcat itself, will use it for their own needs as well.
I don't think that there is a way to get working directory in web-app. However you may use ServletContext.getRealPath()
method to get the real path corresponding to the given virtual path.
If you think you need it, think again.
What if the root of your application is a WAR file and not a "directory"?
Please describe what you think you need it for. Perhaps there's a better solution to the problem you're having than getting an absolute directory path.
I missed the bit about config files. The answer below is correct: Put them in WEB-INF/classes; getResourceAsStream()
is your friend for reading the contents.
This will work with WAR files, exploded or not. It keeps your web app nice and portable.
To store some config settings without adding javax.servlet
to my project I built a string to the config directory & my config file with:
import java.io.file;
String webAppRoot = System.getProperty( "catalina.base" );
String s = File.separator;
String configDir = webAppRoot + s + "webapps" + s + "myApp" + s + "WEB-INF" + s + "config";
String configFile = configDir + s + "myApp.config";
Using File.separator
ensures this solution works on both Windows & Linux.