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

Tomcat config properties file

Poorav Chaudhari
Ranch Hand

Joined: Sep 17, 2002
Posts: 50
Hello,
I have developed a web-application based on the struts framework. In my application I have one config file, which has absolute paths to folders, some passwords etc. So far in the application I have been accessing all this information via an object that i have created, AppProperties. In the AppProperties I have hard coded the path to the properties file. This obviously is a problem at the time of deployment. Below is the code for the AppProperties constructor.

and then I have a getProperties method that will return the property value.
Can someone suggest way, i can overcome this problem. you should also know that I am not passing my servlet context to all the objects. What can I do to this object (and if necessary in the web.xml file) and yet not affect the rest of the application.
please send me all your thoughts. If you think i should code some other way, please let me know that as well.
Thanks
Poorav


Poorav Chaudhari
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
I think the most flexible ways to handle this are either:
1. have a single parameter in the web.xml for your application that passes in the full path where the config file is found.
-or-
2. place the config file under the WEB-INF directory and use the ServletContext getResoureAsStream method to readthe file.
Bill
Poorav Chaudhari
Ranch Hand

Joined: Sep 17, 2002
Posts: 50
If you don't mind, can you please explain to me how i could achieve both of these.
1. have a single parameter in the web.xml for your application that passes in the full path where the config file is found.
-- in this case don't I need a servlet to read the parameter. And if that is the case how can i use my AppProperties java file across the board without having to explicitely pass it
-or-
2. place the config file under the WEB-INF directory and use the ServletContext getResoureAsStream method to readthe file.
-- how can i achieve this from a java file to which i am not passing any servlet. can i create a servlet context in the AppProperties file without passing any information to it?
I know these are very elementary questions, but I have been confused about this for a while, and would really appreciate your help with this.
Thanks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
how can i achieve this from a java file to which i am not passing any servlet. can i create a servlet context in the AppProperties file without passing any information to it?

If this was my problem I would make the constructor for AppProperties take an InputStream rather than having the constructor open a file. That way you could use it with any of the marvelous array of classes in the java.io package that extend InputStream in addition to the ServletContext getResourceAsStream.
Bill
Poorav Chaudhari
Ranch Hand

Joined: Sep 17, 2002
Posts: 50
Thanks for the input Bill, but I am sorry if I am missing the point. Now, say I was to pass an inputstream in the constructor. I am using the App.Properties file in a java class that is called by a class that in turn is called by a servlet. I am not passing any servlet information to this class, so how can i use the ServletContext getResourceAsStream. Or should I define AppProperties as a servlet?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
1. In your servlet - open an InputStream to the config file living in the WEB-INF directory OR from a path passed in as an init parameter.
2. Pass that InputStream through however many objects/classes it takes to get to your AppProperties constructor.
- OR -
2. Create the AppProperties object in the servlet init method and pass it to wherever it needs to be.
In my opinion, the best design principle is to restrict knowledge of the ServletContext to the servlet itself and make all support classes use more general Java - such as InputStream.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tomcat config properties file
 
Similar Threads
FileNotFoundException with specifying Properties file
help me with specify properties file please
unusual exception with properties file path --- please help
Best Practice to load log4j in servlet app
Exception with specifying properties file path in application