• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help loading XML config file via Init Servlet

 
James Dekker
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
James Dekker
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Dekker
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic