wood burning stoves 2.0*
The moose likes Tomcat and the fly likes Help loading XML config file via Init Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Help loading XML config file via Init Servlet" Watch "Help loading XML config file via Init Servlet" New topic
Author

Help loading XML config file via Init Servlet

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 219
Hello,

I am using the Apache Commons Digester to load an XML config file via a servlet with init params...

I keep getting this error when I start Tomcat 5.5.9 and have deployed the war file via my build script:



As one can see there's an obvious pathing issue:



As one can see there's an obvious pathing issue.

Where does the tomcat_home/bin/... come from? The "bin" directory is not supposed to be there!

Here's the specific target from my build script which places attributes-config.xml into tomcat_home/WEB-INF/classes/org/coffeebreak/config:



Here's my deployment descriptor (web.xml) file:



Here's my init servlet for the XML file:



My helper file (which is showing that the rules file is not loading):



What could I possibly be doing wrong?

Why is it stating this when I start Tomcat:



it should be without the "bin" directory, like this:



Why is the rules file coming out as null?



Sincerely,

James Dekker
[ January 14, 2007: Message edited by: James Dekker ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
In web applications, never use relative paths to files, always absolute paths. What the relative path might be is unspecifed, ans sometimes unpredictable, and it varies from one container to the next.

You can convert a relative path to an absolute one like this:

absolutePath = conf.getServletContext().getRealPath("/") + relativePath
[ January 14, 2007: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 219
Mr. Dittmer,

The problem with getRealPath() is that if you set unpackWars="false" then the file returns as null. getRealPath() is raw File I/O.

What am I doing wrong with my code? Why isn't it finding the xml config file?

Sincerely,

JD
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

If you are not unpacking the wars, then you cannot expect the files to live anywhere on the file-system. If you need the files in the file system, don't use unpacked wars.

If you want to read a resource without unpacking, you may wish to explore the getResourceAsStream() method of ServletContext.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 219
Bear,

When I tried your suggestion (using getServletContext()), I got a different error...

Here's my change:



Here's the error message:



When I tried to use the getResourceAsStream() method, by doing this:



I got this error message in Eclipse:



Thank you for your insight!

Sincerely,

James
[ January 14, 2007: Message edited by: James Dekker ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help loading XML config file via Init Servlet