4

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.

subzero
  • 3,420
  • 5
  • 31
  • 40
  • possible duplicate of [The working folder for a Tomcat servlet](http://stackoverflow.com/questions/6584054/the-working-folder-for-a-tomcat-servlet) – daveloyall Nov 21 '14 at 00:50

5 Answers5

5

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 ...

rfeak
  • 8,124
  • 29
  • 28
  • This is the correct answer; I've voted it up. I'd recommend that the OP vote it up and accept it, too. – duffymo Dec 07 '11 at 03:00
4

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.

Pavel S.
  • 1,202
  • 1
  • 13
  • 29
3

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.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
2

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

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.

Stuart Cardall
  • 2,099
  • 24
  • 18