You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Colin Evans <ce...@mail.bitmo.com> on 2000/04/06 23:19:49 UTC

Servlet WAR file and path names

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!

Thanks!
-Colin EVans

--
Colin Evans
Bitmo Corp.
Breaking the Browser Barrier!

261 Jersey St.
San Francisco, CA 94114
415-641-7568 tel
www.bitmo.com





Re: Servlet WAR file and path names

Posted by Jason Hunter <jh...@acm.org>.
(Moved to tomcat-dev from tomcat-user)

Craig R. McClanahan wrote:
> 
> although there's no restriction on a particular URL returned by
> getResource() supporting write access (that's what an HTTP PUT
> normally does, after all).

But as I said, the file URLs used by Tomcat (and standard in the JDK)
don't support write.  So it's not a general solution for local files.

> For short term temporary use, a servlet container is required to

But not all files are temporary.  Some should withstand a server
restart.

> For long term storage, the current recommendation is to use
> databases, directory servers, EJBs, or some other resource that is
> accessible from a servlet based application.  It's not obvious that
> the servlet API itself needs to provide this facility, given the 
> other options available.

So if an author were to, say, write a Counter servlet in Chapter 3 that
persisted its count using init/destroy as a way to demonstrate the
servlet lifecycle, that author would need to write code accessing a
database?  Seems like overkill.

It's not just a "textbook" issue.  Lots of sites don't use databases. 
See this month's JavaWorld for an article by Alex Chaffee proposing
people use XML files instead for small to medium data storage needs to
avoid the overhead and complexity inherent in using an RDBMS.  It'd be
nice if we had a story to support that.  

Originally the URL mechanism was going to allow writes.  That helped
convince us on the expert group it was the right design.  But Tomcat
isn't implementing write capability for files.  I'm wondering, should
we?  Should we try to use the URL mechanism for writing?  How would we
deal with synchronization?  Is there something better API 2.3 could
provide?

-jh-

Re: Servlet WAR file and path names

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Jason Hunter wrote:

> > > 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");
>
> Now the $64,000 question is: What can you do to have the servlet make
> a *change* to the configuration file under WEB-INF?  The default "file:"
> URL in Tomcat doesn't support "output", nor would URL even have a good
> API for writing.  Seems to me we have a read-only solution.  There's no
> good place to store a webapp's long-term writable files.
>

There's no "write" access on getResourceAsStream(), so the question stands,
although there's no restriction on a particular URL returned by getResource()
supporting write access (that's what an HTTP PUT normally does, after all).

For short term temporary use, a servlet container is required to provide the
java.io.File object pointing to a writeable directory, under context attribute
"javax.servlet.context.tempdir" (Servlet API Specification, version 2.2,
section 4.7).  For long term storage, the current recommendation is to use
databases, directory servers, EJBs, or some other resource that is accessible
from a servlet based application.  It's not obvious that the servlet API
itself needs to provide this facility, given the other options available.

>
> -jh-
>

Craig McClanahan



Re: Servlet WAR file and path names

Posted by Jason Hunter <jh...@acm.org>.
> > 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");

Now the $64,000 question is: What can you do to have the servlet make 
a *change* to the configuration file under WEB-INF?  The default "file:"
URL in Tomcat doesn't support "output", nor would URL even have a good
API for writing.  Seems to me we have a read-only solution.  There's no
good place to store a webapp's long-term writable files.

-jh-

Re: Servlet WAR file and path names

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
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



Re: Servlet WAR file and path names

Posted by Hans Bergsten <ha...@gefionsoftware.com>.
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!

Take a look at the ServletContext getResource() and getResourceAsStream()
methods. The give you access to resources specified as paths relative to
the context path, so it should give you the portability you need.

Hans
-- 
Hans Bergsten		hans@gefionsoftware.com
Gefion Software		http://www.gefionsoftware.com