Link to home
Start Free TrialLog in
Avatar of Juuno
Juuno

asked on

Get filepath in web application

Hi,

How can I get the path of my web application?

For example,  i'm working on eclipse and the name of my web application is Test which path is "C:\Documents and Settings\user\workspace\Test". And I have my xml files in  "C:\Documents and Settings\user\workspace\Test\WebContent\WEB-INf\Classes\Resources\". How I  can do to get that path from my javabean which i can't extend it to the HttpServlet class.
Avatar of a_b
a_b

Try this, it gives you the path which you are executing your program -
public class GetExecutionPath
{
    public static void main(String args[])
    {
	try
	{
	    String executionPath = System.getProperty("user.dir");
	    System.out.print(executionPath);
	} catch (Exception e)
	{
	    
	}
    }
}

Open in new window

Avatar of Juuno

ASKER

I've already tried it.
I can get the file path if my program is stand alone.
For web application, I just got C:\Eclipse, since user.dir contains the current working directory (ie. where the vm was run from).
But you want the location of the xml?
Avatar of Juuno

ASKER

It is like I want to retrieve xml from my java bean, so, I want to get that path to my java bean. In my standard program. I used this:

static String curDir = System.getProperty("user.dir");
static String resources = ("\\WebContent\\WEB-INF\\classes\\resources");
and I give the path for my xml file like this
File file = new File(curDir + resources + "myxml.xml") and it works.

But for my web application, System.getProperty("user.dir") only returns C:\Eclipse where VM runs.

So, I want to know how to get file path in web application.
Avatar of Juuno

ASKER

Thanks.
But my JavaBean program can't extend to Servletcontext.
you'll need to pass it a context (or request) to determine the path
>But my JavaBean program can't extend to Servletcontext.
You are right. But you could create a load-on-startup servlet. In its init method  you could set the ServletContext into a field of a static utility class.  Your javabean could access that.  
 
Avatar of Juuno

ASKER

Thanks.

Can you explain me a bit more about load-on-startup servlet. How can I do this?
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial