If you don't need a File
object, you can use ServletContext.getResource(String path)
. The path
argument must begin with '/', and is relative to context root. The method returns a URL
, which you can open and then read the contents of the file.
I don't think there is a standard method of getting a pointer to a File
object. You could always use a system property, but that's probably no better than putting it in the web.xml
.
UPDATE
If you really need a File
object, and not just the contents of the file, there are a couple of things you can do. It depends on how the location of the file changes.
If the file location changes based on the server you are deploying to, but it stays the same on each server, then just add a system property to your web container's startup scripts. How you do this, of course, varies by container, but you should be able to find out how in the documentation.
You could also put the location of the file in a separate properties file, say file.properties
, and modify your deployment process to generate or update this file and place it in your WEB-INF
directory. Then you can read this properties file with ServletContext.getResourceAsStream()
, get the path, and instantiate the File
.