Using relative path in java programs - how ?

Discussion in 'Java' started by Guy, Jun 15, 2004.

  1. Guy

    Guy Guest

    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 ?
     
    Guy, Jun 15, 2004
    #1
    1. Advertising

  2. Guy

    Dan Gravell Guest

    Guy wrote:

    > 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
     
    Dan Gravell, Jun 15, 2004
    #2
    1. Advertising

  3. 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);

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Jun 15, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    3
    Views:
    19,571
    Sudsy
    Jul 8, 2004
  2. thefirstwml
    Replies:
    1
    Views:
    2,378
    Edwin Dankert
    Apr 19, 2004
  3. Thomas Guettler
    Replies:
    3
    Views:
    762
    Andrei
    Oct 27, 2003
  4. Oren
    Replies:
    1
    Views:
    552
    Michael D. Ober
    Apr 29, 2007
  5. Nigel Wilkinson
    Replies:
    2
    Views:
    390
    Nigel Wilkinson
    Jul 25, 2005
Loading...

Share This Page