• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Context Listener

 
Jonathan Cone
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a web application that utilizes a context listener to instantiate an object that parses data from a raw text file and inserts it into a data structure. The reference to this data structure object is then stored in a context attribute.

The deployment problem that I am having is that when the context is initialized, my application cannot find the text file. Here's my code:

From the DD:




The listener class:


The actual file reader class:


I placed the rates directory directly under the application directory. Based on my code, is this where it should be or am I pointing to something else? Any tips would be appreciated.
[ August 08, 2005: Message edited by: Jon Cone ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out ServletContext.getRealPath()
 
Jonathan Cone
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear I really appreciate it -- problem solved!
 
Yi Si
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should use real path to read file as above post mentioned.

In addition, you can use follow method if your file is located under document root of your web app.

ServletContext java.io.InputStream getResourceAsStream(java.lang.String path)
Although the path should be start with "/" , it is not absolute path. It is relative to your document root of your web app.
 
Ralph Ma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should use getResourceAsStream instead of your own method.
Because this method uses ClassLoader to get stream, if your application is packaged as a war, this method will work very well while yours may not work
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic