• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Load a configuration file

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
slicker
Posts: 1108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why dont you load the servlet class at the server startup using the listener tag in web.xml?
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic