Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Load a configuration file

 
Saumya Nair
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
load you file in the init() method of your servlet so it gets done once, for the life of the servlet.
 
Saumya Nair
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you load the servlet class at the server startup using the listener tag in web.xml?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic