• 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

Can I pick your brains on globally accessing XML resources?

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my servlet/JSP app I have a handful of XML files which all sit in /WEB-INF and get accessed from various different places (Filters, Controllers, Beans...). I started out using servletContext.getResourceAsStream() and then using DocumentBuilder to create a new Document object.

The problem with that was that I was:

a) Needing to pass ServletContext around all components which need to read XML files
b) Creating multiple instances of the same Document needlessly.

So with that in mind I set about trying to create a singleton called DocumentFactory which contains ServletContext and a Map of already built Documents.

Early on in the FrontController invoke: DocumentFactory.getInstance().init(servletContext).



This worked great when I was just working within my servlet. But just recently I tried using it in a JavaBean from a JSP file. It always returns NULL for any document that wasn't used previously. The only difference between trying to use it from a JSP/JavaBean and using it from the servlet is that RequestDispatcher.forward() has been called.

I've pinned the problem down to this line:



Is there a reason why servletContext (from the Servlet, before forward() was called) will not fetch resources *after* forward() has been called?

Is that a more elegant way to do what I'm trying to do?

Basically I just want a convenience class that can grab me a XML file from WEB-INF and can be accessed from anywhere within the current context.

Thanks for the help if anybody actualy gets to the end of this thread
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EDIT | Scrap that. Confused
[ September 09, 2007: Message edited by: Chris Corbyn ]
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Drum roll please.....

<navigation>
...
</naviagtion>

That was the root of my problem. I've got fat fingers. Now suddenly I don't care if I have to put try/catch around every call to DocumentFactory.getDocument() because I'd rather know what the problem is than just having a null return value
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic