• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

accessing a file from a servlet

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andy,
I've placed my properties file in a directory under Web-inf. Now i'm getting the path to the file.


You can then use getResourceAsStream("/WEB-INF/myproperties.properties") to return you an InputStream that you can pass to the property.load() method.


Which class does the method 'getResourceAsStream' belong to? I checked java.lang.Class and java.lang.ClassLoader classes. The concept of ClassLoader confuses me.Could you please explain about it? Also, please give me the code for calling getResourceAsStream. I mean, isn't it like
someObject.getResourceAsStream("/WEB-INF/myproperties.properties")
Thanx in advance,
Tony
[ October 11, 2003: Message edited by: tony james ]
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony
Best way is to put the properties file into a directory in the deployed WAR file. I prefer to put it below the WEB-INF directory as then it cannot be viewed by typing the URL into a browser.
You can then use getResourceAsStream("/WEB-INF/myproperties.properties") to return you an InputStream that you can pass to the property.load() method.
HTH
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The file is relative to your web-xml file. If the entry looks like this, ie in the same directory as web-xml:
<init-param>
<param-name>filePath</param-name>
<param-value>./file.prop</param-value>
</init-param>
Then in your doGet or doPost, you can quickly test it like this:
out.println("file = " + getServletConfig().getInitParameter("filePath"));
This will print:
file = ./file.prop
[ October 10, 2003: Message edited by: Roger Chung-Wee ]
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually, I think '.' doesn't guarantee "from WEB-INF", so much as it guarantees 'the current directory'... so if I invoked my java command that started tomcat from /usr/local/tomcat , that's what '.' would give me.

Someone prove me wrong though.
 
tony james
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andy,
I've placed my properties file in a directory under Web-inf. Now i'm getting the path to the file.


You can then use getResourceAsStream("/WEB-INF/myproperties.properties") to return you an InputStream that you can pass to the property.load() method.


Which class does the method 'getResourceAsStream' belong to? I checked java.lang.Class and java.lang.ClassLoader classes. The concept of ClassLoader confuses me.Could you please explain about it? Also, please give me the code for calling getResourceAsStream. I mean, isn't it like
someObject.getResourceAsStream("/WEB-INF/myproperties.properties")
What object should i use here?
Thanx in advance,
Tony
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi tony,

please Don't edit your original posting. It confuses the heck out of people because now the first posting (the original question) seems to have nothing to do with the following answers.

But anyways..

Quick lesson on API usage: When you're browsing the javadocs, click the "index" link that's at the very top of every page. Then click the first letter of the method name you want (in your case 'g'), and use your browser's Find function to find 'getResourceAsStream'

You will find it's on the javax.servlet.ServletContext interface.

Don't know how to get a ServletContext? try searching for 'getServletContext'.

As for classloading.. getResourceAsStream apparently doesn't use it. (or so the API says).
[ October 13, 2003: Message edited by: Mike Curwen ]
 
tony james
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike,
I accept editing the original post was a big mistake. Will make sure that it doesn't happen again. And thanks a lot for the API browsing tip.
Am still not able to read values from my properties file. I've placed it in TOMCAT_HOME\webapps\sample\WEB-INF\properties\test.properties
My server.xml entry is
<Context path="/sample" docBase="sample" debug="0"
reloadable="true" crossContext="true">
I am getting the ServletContext object using getServletContext().
But getServletContext().getResourceAsStream("/Web-inf/properties/test.properties")) returns null. What could be wrong?
Thanks
Tony
 
tony james
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Everythings working fine when i replaced getResourceAsStream("/Web-inf/properties/test.properties")) with WEB-INF
Thanks for the help anyway,
Tony
 
Normally trees don't drive trucks. Does this tiny ad have a license?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic