File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Context Listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Context Listener" Watch "Context Listener" New topic
Author

Context Listener

Jonathan Cone
Greenhorn

Joined: May 22, 2005
Posts: 24
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

Joined: Jan 10, 2002
Posts: 61413
    
  67

Check out ServletContext.getRealPath()


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jonathan Cone
Greenhorn

Joined: May 22, 2005
Posts: 24
Thanks Bear I really appreciate it -- problem solved!
Yi Si
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
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.


Sun(java)-SCJA SCJP SCWCD SCBCD SCDJWS SCEA<br />Sun(solaris)-SCSA SCSN<br />IBM-486 Object-Oriented Analysis and Design with UML Test <br />IBM Certified System Administrator - WebSphere Application Server Network Deployment V6.0<br />Oracle-OCA
Ralph Ma
Greenhorn

Joined: Aug 18, 2005
Posts: 10
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


SCJP 1.2<br />SCWCD 1.3<br />SCBCD 1.3<br />SCDJWS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Context Listener