Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help loading XML config file via Init Servlet

 
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic