• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Relative Path to data

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

Assume that I have a file /WEB-INF/data/categories.txt

For obvious reasons I don't want to hard-code a path to the data, but instead want to use a path relative to the application directory. How do I specify that relative path, either within the JSP or within web.xml?

Thanks!

[Edit]It would help if I specified that the app is running under Tomcat 6, but I can't use CATALINA_HOME since that is pointing to Tomcat 5.
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That depends what you are trying to do.
 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I didn't mean to be cryptic. The idea is to read from two text files, one containing categories and one containing numbers in text form. These values are displayed by the app. New values are entered in a third column, and those values get written to a third text file. All three files are in the data directory. Does that clarify the situation?
 
Bear Bibeault
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best way to get the contents of the files is to use ServletContext.getResourceAsStream().
 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, forgive the ignorant question, but how do I set/determine the path to the files?
 
Bear Bibeault
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Relative to the application root.
 
Bear Bibeault
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Javadoc:

The path must begin with a / and is interpreted as relative to the current context root

 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me give a code fragment. The lower code works fine, but obviously ties this to the very specific configuration of my computer. I have tried many variations of the code commented out, such as "/WEB-INF/data/new.txt" I had naively thought that one or another of these would define the path relative to the context root. So here I sit with a dreadful example of hard coding. I think I may need a little spoon feeding here. Thanks!


 
Bear Bibeault
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see no use of getResourceAsStream() in your code.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following will probably work.
getResourceAsStream works for reading the files.
to write to the file, you need to get a handle on it with the getRealPath method.

Also fixed variable naming to comply with conventions (lower case letter for variables)
Note that writing to files under your web application directory is not necessarily supported.
Suggestion: put the config file seperate from the web application, so that it won't get over-written at each deployment.


 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stefan,

Thank you very much, that did the trick (so now hopefully I will know the trick).

-- Ed
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic