tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: Servlet WAR file and path names
Date Thu, 06 Apr 2000 21:27:49 GMT
Colin Evans wrote:

> Hi, I'm currently writing a servlet application that loads several .XSL XSLT
> files for processing XML data, and I would like to bundle these .XSL files
> in a WAR archive so that the application can be deployed in one peice.
> However, I cannot figure out how to find or generate a file path to
> reference these files relative to the location of the WAR distributable.  I
> always end up entering the file locations by hand for each deployment, or
> entering them as parameters in the web.xml application config file.
> Does Tomcat have a way to give names to non-JSP and servlet resources, or to
> report the path location of a servlet's web application location?
> Otherwise, it makes including additional support files very difficult.  Any
> help or suggestions would be appreciated!

The convention recommended in the servlet specification is to use
ServletContext.getResource() or ServletContext.getResourceAsStream() to access
things like this, rather than doing file i/o.  That way, your web app will still
work even in an advanced servlet container that does not run out of the

The conventional place to store things like this is in the WEB-INF directory.
So if you stored such a file at:


you could access it in a servlet (or JSP page) like this:

    InputStream stream =


  URL url = getServletContext().getResource("/WEB-INF/mystylesheet.xsl");
  ... use the URL.openConnection() method to get an input stream ...

One nice advantage of storing files like this in the WEB-INF directory is that
they cannot be accessed via a URL by a client browser (because the servlet
container is specifically prohibited from serving files out of WEB-INF for
security reasons).  If you needed these files to be accessible to clients also,
you should store them under the document root (or a subdirectory under it), and
you can still use getResource() to retrieve them from within your server-side

> Thanks!
> -Colin EVans

Craig McClanahan

View raw message