tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <Craig.McClana...@eng.sun.com>
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
filesystem.

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

    WEB-INF/mystylesheet.xsl

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

    InputStream stream =
getServletContext().getResource("/WEB-INF/mystylesheet.xsl");

or

  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
application.

>
> Thanks!
> -Colin EVans
>

Craig McClanahan



Mime
View raw message