• Post Reply Bookmark Topic Watch Topic
  • New Topic

Relative Path to data  RSS feed

 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
OK, forgive the ignorant question, but how do I set/determine the path to the files?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Relative to the application root.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see no use of getResourceAsStream() in your code.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Stefan,

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

-- Ed
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!