GeeCON Prague 2014*
The moose likes Servlets and the fly likes Load a configuration file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Load a configuration file" Watch "Load a configuration file" New topic
Author

Load a configuration file

Saumya Nair
Ranch Hand

Joined: Apr 25, 2006
Posts: 34
Hi All,


I am calling a java file (servlet) from my JSP... I need to load a configuration file on the servlet call.. How can I do it ??

I tried using new AppProperties(name,filename) , but the file does not load. I am using hibernate and mysql. Every time my servlet is being called it throws invalid URL exception for the above line..

Please help...


Regards,
Saumya
John Dunn
slicker
Ranch Hand

Joined: Jan 30, 2003
Posts: 1108
load you file in the init() method of your servlet so it gets done once, for the life of the servlet.


"No one appreciates the very special genius of your conversation as the dog does."
Saumya Nair
Ranch Hand

Joined: Apr 25, 2006
Posts: 34
When i print the Property i get from the file, it does not print the ones with the empty tags.

i.e

<property name="hibernate.config" value="/hibernate.cfg.xml" />
<property name="log4j.config" value="config/log4j.xml" />


is there any way i can load this configuration file (cfg.xml) file on the server startup. I m using Tomcat.


Regards,
Saumya
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Why dont you load the servlet class at the server startup using the listener tag in web.xml?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Within a web app I use the following to load configuration file to a property object.

InputStream in = this.class.getClassLoader().getResourceAsStream("/WB-INF/myProp.properties");

OR,

InputStream in = servletContext.getClassLoader().getResourceAsStream("/WB-INF/myProp.properties");


Properties p = new Properties();
p.load(in);

My webapplication context name is "myApp" and the config file myProp.properties is there in the WEB-INF folder of the application.


Rahul Bhattacharjee
LinkedIn - Blog
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
To take the configuration filename declaratively .you can set the name of the file as servlet init params.
And then load the file from the location to a properties object.
 
GeeCON Prague 2014
 
subject: Load a configuration file