Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading properties file into Servlet  RSS feed

 
Vamsi Vetcha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai all,

I want to open a .properties extension file from Servlet to get DataBase URL,Credentails etc.

I did like this

Properties prop = new Properties();
FileInputStream objFileInput = new FileInputStream("NamedQuerys.properties");
prop.load(objFileInput);

i am getting this error

java.io.FileNotFoundException: NamedQuerys.properties (The system cannot find the file specified)

i placed NamedQuerys.properties file in classes folder of WEB-INF
and the servlet which uses the above code also placed in classes folder only.

Plz say where i have to place the .properties extension file to call from the servlet ,and need i have to specify any thing in web.xml file
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new FileInputStream("NamedQuerys.properties");

This looks for the file in the 'current directory', the problem is that the term 'current directory' keeps changing

You should have a look at this thread.

Dave
 
Vamsi Vetcha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you David,

I got the answer and i used getResourceAsStream by placing .properties extension file in more folder with Context to Web application name i.e

sample(this is web application name)/more/DataBaseKeysInfo.properties

InputStream is = getServletContext().getResourceAsStream("/more/DataBaseKeysInfo.properties");
Properties prop = new Properties();
prop.load(is);

now it works perfectly.
 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In future if you are not using servlet you can think of using
Thread.currentThread().getContextClassLoader().getResourceAsStream("file name").
By putting file in the main server directory.
 
Choon-Chern Lim
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use PropertyResourceBundle like this:-



The beauty with this is you don't have to specify relative/absolute path as argument. You specify your package-style path to your property file.

So, if you have "myprop.properties" sitting in package org.myproj, then you specify "org.myproj.myprop" and truncate the ".properties" part.

I bet your property file looks like this:-


To get the value, do this:


Hope this helps.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!