Getting absolute path of a file in a webapp without using servlet/JSP
Hi all,
I need a small clarification. Is it possible to reterive the absolute path of a file present in a tomcat web application without using Servlet/JSP. I have a normal java class which uses this file to read the configuration parameters. I like to know whether I need to create a seperate servlet which will read the parameters from web.xml.
> Hi all,
> I need a small clarification. Is it possible to
> reterive the absolute path of a file present in a
> tomcat web application without using Servlet/JSP.
What if the file is in a WAR? What do you do then?
> I
> have a normal java class which uses this file to
> read the configuration parameters.
There are other, better ways to do this.
> I like to know
> whether I need to create a seperate servlet which
> will read the parameters from web.xml.
What do you really want it for? You shouldn't need an absolute path. Use the class loader to get an InputStream.
%
Hi,
Thanks for your reply. The main reason I need this is to load a property file (apps.xml) present inside WEB-INF/classes directory of my web application. I am using this file only once in my class. I also tried using getResourceAsStream method as shown below:
InputStream is=getClass().getClassLoader().getResourceAsStream("WEB-INF/classes/apps.xml");
also tried
is=getClass().getResourceAsStream("/WEB-INF/classes/apps.xml");
But in both cases it returns null. Please help me if you find any fault in my above approach or is there any better approach than this
Hi,
I found out the mistake I made before. Actually, I must give the path without WEB-INF/classes as by default it searches from there. So my code must be :
InputStream is = getClass( ).getResourceAsStream("apps.xml");
Now its working fine for me. Thanks a lot