Using relative path in java programs - how ?

G

Guy

Hi

I wrote a new servlet that uses a configuration file to store
parameters.
The problem is that I want to use a relative path when accessing this
file, since I dont know where this servlet will be deployed.
Basically, I want it to be in the same directory where the .class
files are located.

Using the property user.dir does'nt help because it returns the
server's executable path.

How can I control this ? how can the program "know" where the class
files are ?
 
D

Dan Gravell

Guy said:
Hi

I wrote a new servlet that uses a configuration file to store
parameters.
The problem is that I want to use a relative path when accessing this
file, since I dont know where this servlet will be deployed.
Basically, I want it to be in the same directory where the .class
files are located.

Using the property user.dir does'nt help because it returns the
server's executable path.

How can I control this ? how can the program "know" where the class
files are ?

Look at the ServletContext object, which you should be able to get from
your Servlet. You normally place the config file somewhere like WEB-INF
and then do a getResource() or getResourceAsStream() (sorry I can't
remember specifics, it's been a while).

Dan
 
A

Andrew Thompson

On 15 Jun 2004 00:59:00 -0700, Guy wrote:
...
I wrote a new servlet that uses a configuration file to store
parameters.
The problem is that I want to use a relative path when accessing this
file, since I dont know where this servlet will be deployed.

Put the config. file in a known place on the
server (the servlet is another matter), then..
URL url = getServletContext().getResource(fileName);

But then, I understand the accepted way to
configure servlets is by adding the information
to the deployment descriptor and invoking...

String param = ServletContext.getInitParameter(name);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top