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
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
And then we all jump out and yell "surprise! we got you this tiny ad!"