This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).
This tends to bite people when they move from a development environment where the app is deployed as an "exploded" directory to a production or test environment where it's deployed as a WAR.
Mazza, Glen R., ,CPMS wrote:
Thanks to both--I implemented Mike's solution (also placed the stylesheet more properly under the web-inf directory.) It worked fine. (ServletContext.getResourceAsStream() is also a good solution--thanks.)
Glen
-----Original Message----- From: Kris Schneider [mailto:[EMAIL PROTECTED] Sent: Monday, July 07, 2003 2:08 PM To: Struts Users Mailing List Subject: Re: resolving relative paths to servet webapp/myapp directory
I tend to use ClassLoader.getResource or ClassLoader.getResourceAsStream to load resources. The constraint, obviously, is that the class loader must be able to access the resources. Generally, this means placing resources under WEB-INF/classes or in JAR files in WEB-INF/lib. If you don't care about depending on the Servlet API, try ServletContext.getResource or ServletContext.getResourceAsStream. In your particular case, you'd use:
InputStream in = context.getResourceAsStream("/stylesheets/mystylesheet.xsl");
Quoting Mike Deegan <[EMAIL PROTECTED]>:
we do something similar but we use the WEB-INF directory as the starting point and all works fine lets say we are looking for "XML/extraction-map-classday.xml"
the following code achieves this ...
public final class HomePageAction extends Action ... ... String xmlURI =
this.getServlet().getServletContext().getRealPath("/WEB-INF/XML/extraction-m
ap-classday.xml");
Therefore the "RealPath" to extraction-map-classday.xml = the value in xmlURI
if you had "stylesheets\mystylesheet.xsl" under the WEB-INF directory
maybe
you could follow or approach
i know it works for us but maybe we just got lucky !!!
HTH Mike
----- Original Message ----- From: "Mazza, Glen R., ,CPMS" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, July 07, 2003 11:38 AM
Subject: resolving relative paths to servet webapp/myapp directory
Hello,
I'm doing an XML->PDF XSLT transformation using a XSL stylesheet.
Programmatically, within my Action class, I refer to the stylesheet
needed
by using a relative path, with the goal of it being relative to my
appname
directory off of the servlet "webapps" directory:
TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource("stylesheets\\mystylesheet.xsl")); ..
Problem is, Struts appears to be resolving relative paths from my
project
working directory, *not* relative to the "appname" from the webapps directory in Tomcat.
To confirm this, removing the xsl stylesheet from my working directory returned this error when I ran the app in Tomcat:
java.io.FileNotFoundException: D:\myworkingdir\myapp\stylesheets\mystylesheet.xsl
and not this error:
java.io.FileNotFoundException: D:\tomcat4.1\webapps\myapp\stylesheets\mystylesheet.xsl
How do I specify filenames in Struts so they will be resolved relative
to
the webapps\myapp directory (whatever I name "myapp" to be), and not my (deleteable) working directory?
Thanks, Glen
-- Kris Schneider <mailto:[EMAIL PROTECTED]> D.O.Tech <http://www.dotech.com/>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]

