Re: getResourceAsStream(path)

Figured it out!  I also found it to be a very cool way of providing property files for 
web-apps and have them deployed with the application (under WEB-INF/properties).

To load a file from within the webapp context using getResourceAsStream(path):

The ServletContext.getResourceAsStream(pathToResource) starts at the getContextPath() 
as the path root.

For example, if you have a file Headache.xml in the following directory hiearchy:

/approot/
        /WEB-INF
                /guidelines
                        Headache.xml

You use the following to read from a file programmatically within a servlet:

                ServletContext sc = this.getServletContext();
                InputStream is_xml = 
sc.getResourceAsStream("WEB-INF/guidelines/Headache.xml");

One odd thing with JRun3:
The sc.getContextPath() returns "/approot"  without the second slash. Not sure why I 
don't need a /WEB-INF when using getResourceAsStream() but it wouldn't work that way. 
It seems to add the second slash. The specification only states that it must start 
with a slash, so there is some vagueness in the spec:

--------------------------------------------------------------------------------------------------------------------------------------------------------
public java.net.URL getResource(java.lang.String path)
                         throws java.net.MalformedURLException

        Returns a URL to the resource that is mapped to a specified path. The path 
must begin with a "/" and is interpreted as relative to the current context root.
        This method allows the servlet container to make a resource available to 
servlets from any source. Resources can be located on a local or remote file system, 
in a      database, or in a .war file.

        The servlet container must implement the URL handlers and URLConnection 
objects that are necessary to access the resource.
-----------------------------------------------------------------------------------------------------------------------------------------------------

For those curious as to what I am doing, I'm using Apache's Xalan to perform XSL 
Transformation on an XML file using a particular XSL stylesheet. Both the XML and XSL 
files are loaded programmatically and the XSLT processor invoked. We starting 
constructing medical guidelines in XML format and are exploring the use of XSLT to 
output them in a variety of formats including HTML "on the fly".  Pretty slow when it 
starts, but works nicely once it is loaded and running in the servlet container.

One thing I did notice was that the XSLT Processor chokes on URLs that have multiple 
variables (using an ampersand) to be passed to servlets. Apparently, an ampersand does 
not make for a well formed XML document.

--Mike H

------------------------------------------------------------------------------
Archives: http://www.egroups.com/group/jrun-interest/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebarRsts&bodyRsts/jrun_talk
or send a message to [EMAIL PROTECTED] with 'unsubscribe' in the 
body.

Reply via email to