You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Craig R. McClanahan" <cr...@apache.org> on 2001/06/16 06:16:36 UTC

RE: Accessing a packaged file


On Tue, 24 Apr 2001, Milt Epstein wrote:

> On Mon, 23 Apr 2001, Filip Hanik wrote:
> 
> > > In this case, the class and the resource were packaged together:
> > > that should mean you could use the same class-loader for both, no?
> > > (Assuming, of course, the user didn't monkey around with your
> > > classes,...)
> >
> > this may however not be guaranteed at all times. especially if you
> > monkey:ed around with your classpath, hence it is safer to use the
> > context class loader instead.  that way you don't have to worry,
> > better to play it safe, don't you agree?
> 
> Once again, I ask, what is the difference between the
> Class/ClassLoader getResource* methods and the ServletContext ones?
> Thanks.
> 

This didn't get answered in April, but it's worth an answer for the
archives.

The ServletContext.getResource() methods are used to access static
resources from your web application.  For example, if you want to read the
web.xml file for your own app, the portable way to do this is:

  InputStream stream =
   getServletContext().getResourceAsStream("/WEB-INF/web.xml");

On the other hand, Class.getResource* (and therefore
ClassLoader.getResource*) are used to load resource files from the class
path for your web application (i.e. from unloaded files in
/WEB-INF/classes or inside a JAR file in /WEB-INF/lib).

Craig McClanahan