The Resources Component
Table of Contents
Introduction
The Resources element represents all the resources available to the web application. This includes classes, JAR files, HTML, JSPs and any other files that contribute to the web application. Implementations are provided to use directories, JAR files and WARs as the source of these resources and the resources implementation may be extended to provide support for files stored in other forms such as in a database or a versioned repository.
Resources are cached by default.
Note: Running a webapp with non-filesystem based Resources implementations is only possible when the webapp does not rely on direct filesystem access to its own resources, and uses the methods in the ServletContext interface to access them.
A Resources element MAY be nested inside a Context component. If it is not included, a default filesystem based Resources will be created automatically, which is sufficient for most requirements.
Attributes
Common Attributes
All implementations of Resources support the following attributes:
Attribute | Description |
---|---|
allowLinking |
If the value of this flag is NOTE: This flag MUST NOT be set to true on the Windows platform (or any other OS which does not have a case sensitive filesystem), as it will disable case sensitivity checks, allowing JSP source code disclosure, among other security problems. |
cacheMaxSize |
The maximum size of the static resource cache in kilobytes.
If not specified, the default value is |
cacheObjectMaxSize |
Maximum size of the static resource that will be placed in the cache.
If not specified, the default value is |
cacheTtl |
The amount of time in milliseconds between the revalidation of cache
entries. If not specified, the default value is |
cachingAllowed |
If the value of this flag is |
className |
Java class name of the implementation to use. This class must
implement the |
trackLockedFiles |
Controls whether the track locked files feature is enabled. If
enabled, all calls to methods that return objects that lock a file and
need to be closed to release that lock (e.g.
If not specified, the default value of |
Standard Implementation
The standard implementation of Resources is org.apache.catalina.webresources.StandardRoot. It does not support any additional attributes.
Nested Components
A web application's main resources are defined by the docBase defined for the Context. Additional resources may be made available to the web application by defining one or more nested components.
PreResources
PreResources are searched before the main resources. They will be searched in the order they are defined. To configure PreResources, nest a <PreResources> element inside the <Resources> element with the following attributes:
Attribute | Description |
---|---|
base |
Identifies where the resources to be used are located. This attribute
is required by the |
className |
Java class name of the implementation to use. This class must
implement the |
internalPath |
Identifies the path within the base where the
resources are to be found. This is typically only used with JAR files when
the resources are not located at the root of the JAR as is the case with
resource JARs. This attribute is required by the
|
readOnly |
If |
webAppMount |
Identifies the path within the web application that these resources
will be made available. For the
|
JAR resources
JarResources are searched after the main resources but before the PostResources. They will be searched in the order they are defined. To configure JarResources, nest a <JarResources> element inside the <Resources> element. The configuration attributes are the same as for PreResources.
During web application start, the JAR scanning process checks scanned JARs
for content under /META-INF/resources
. Where found, this static
content is added to the JarResources.
Post-resources
PostResources are searched after the resource JARs. They will be searched in the order they are defined. To configure PostResources, nest a <PostResources> element inside the <Resources> element. The configuration attributes are the same as for PreResources.
Ordering
In addition to the sets of resources described above, the standard
implementation also maintains ClassResources which represent the classes
contained in the JAR files mapped to /WEB-INF/classes
. This
allows other components to search for classes with a single call rather than
one call to search /WEB-INF/classes
followed by another to search
the JARs in /WEB-INF/lib
. The ClassResources are populated
from the JARs mapped to /WEB-INF/lib
when the web application
starts.
Therefore, the complete search order is:
- PreResources
- MainResources
- ClassResources
- JarResources
- PostResources
The population of ClassResources and JarResources at web application start means that care needs to be taken to add JAR based resources correctly to obtain the desired behaviour. Consider the following example:
<Resources>
<PostResources base="D:\Projects\external\classes"
className="org.apache.catalina.webresources.DirResourceSet"
webAppMount="/WEB-INF/classes"/>
<PostResources base="D:\Projects\lib\library1.jar"
className="org.apache.catalina.webresources.FileResourceSet"
webAppMount="/WEB-INF/lib/library1.jar"/>
</Resources>
Since both resources are PostResources, it might be expected that
D:\Projects\external\classes
will be searched for classes before
D:\Projects\lib\library1.jar
. However, by adding the JAR using a
FileResourceSet
, the JAR is mapped to /WEB-INF/lib
and will be processed at application start along with the other JARs in
/WEB-INF/lib
. The classes from the JAR file will be added to the
ClassResources which means they will be searched before the classes from
D:\Projects\external\classes
. If the desired behaviour is that
D:\Projects\external\classes
is searched before
D:\Projects\lib\library1.jar
then a slightly different
configuration is required:
<Resources>
<PostResources base="D:\Projects\external\classes"
className="org.apache.catalina.webresources.DirResourceSet"
webAppMount="/WEB-INF/classes"/>
<PostResources base="D:\Projects\lib\library1.jar"
className="org.apache.catalina.webresources.JarResourceSet"
webAppMount="/WEB-INF/classes"/>
</Resources>
In short, the JAR file should be added as a JarResourceSet
mapped to /WEB-INF/classes
rather than using a
FileResourceSet
mapped to /WEB-INF/lib
.
Special Features
No special features are associated with a Resources element.