File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to read properties files from outside Container? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to read properties files from outside Container?" Watch "How to read properties files from outside Container?" New topic
Author

How to read properties files from outside Container?

Dominic Steng�rd
Ranch Hand

Joined: Feb 05, 2001
Posts: 186
Hi Web Container Gurus!

Ive got a servlet from which I want to load properties/config files. Today these files are located in WEB-INF/classes, but I want to relocate them to a location within the same server that is outside this specific web app or Tomcat installation. My thought is to refer to these files using the File object.

Is this possible, if so how do I do it?
Thanks in advance.

Kind regards

[ August 14, 2007: Message edited by: Dominic Steng�rd ]
[ August 14, 2007: Message edited by: Dominic Steng�rd ]

Dominic Steng�rd<br />Sun Certified Java 2 Programmer
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I would make the path to the properties file a context init param.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Dominic Steng�rd
Ranch Hand

Joined: Feb 05, 2001
Posts: 186
Hi Ben,

Thanks for your quick reply. :-)

Yes I will make it a context init parameter. But my question is more related to if it is even possible for a File object within serlvetA in webappA to reach fileA, even though fileA is not located within webappA? FileA will be located somewhere else on the filesystem.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
Yes, you can do that. However, just remember your app is no longer a simple war, and it has other dependency files that must also be deployed. Often I put properties in a database table. Often times your properties differ depending on if you are in dev/test/prod. If you go to your db connection to get properties you can easily update them at any time with sql (as opposed to ftp and vi for example), and you don't have to remember to deploy different properties files for dev, test, prod or even to set an indicator that you are in dev/test/prod.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Dominic Steng�rd:
Hi Ben,

Thanks for your quick reply. :-)

Yes I will make it a context init parameter. But my question is more related to if it is even possible for a File object within serlvetA in webappA to reach fileA, even though fileA is not located within webappA? FileA will be located somewhere else on the filesystem.


Yes.
You might have issues if the file isn't actually on the same machine as the container (Windows network, NFS, etc..) but if they're on the same machine and you have an absolute path to the file, you shouldn't have a problem.

Other issues could be file permissions, and/or Java security policies, if your container is running under one.
Dominic Steng�rd
Ranch Hand

Joined: Feb 05, 2001
Posts: 186
Thanks for all the help youve provided.
The application is up and running well. :-)
Joseph Kampf
Greenhorn

Joined: Mar 04, 2004
Posts: 26
One method that I like is to put a directory in the classpath. Place the property file in the directory. You can even have a directory structure under that directory to give some sort of context. Then use the Classloader getResourceAsStream to get a stream to the properties file. From there you can load up a Properties instance.

I like this better than having an a priori directory location in a Servlet context, (This is hard to change) or requiring a -D option that contains the path location.


Originally posted by Dominic Steng�rd:
Hi Web Container Gurus!

Ive got a servlet from which I want to load properties/config files. Today these files are located in WEB-INF/classes, but I want to relocate them to a location within the same server that is outside this specific web app or Tomcat installation. My thought is to refer to these files using the File object.

Is this possible, if so how do I do it?
Thanks in advance.

Kind regards

[ August 14, 2007: Message edited by: Dominic Steng�rd ]

[ August 14, 2007: Message edited by: Dominic Steng�rd ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read properties files from outside Container?