This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
Joined: Jan 14, 2007
EDIT | Scrap that. Confused [ September 09, 2007: Message edited by: Chris Corbyn ]
Joined: Jan 14, 2007
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
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Can I pick your brains on globally accessing XML resources?