aspose file tools*
The moose likes Servlets and the fly likes accessing a file from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "accessing a file from a servlet" Watch "accessing a file from a servlet" New topic
Author

accessing a file from a servlet

tony james
Greenhorn

Joined: Jul 24, 2001
Posts: 12
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 ]
Andy Bowes
Ranch Hand

Joined: Jan 14, 2003
Posts: 171
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


Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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 ]

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Jul 24, 2001
Posts: 12
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

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Jul 24, 2001
Posts: 12
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

Joined: Jul 24, 2001
Posts: 12
Hi,
Everythings working fine when i replaced getResourceAsStream("/Web-inf/properties/test.properties")) with WEB-INF
Thanks for the help anyway,
Tony
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing a file from a servlet