Class ServletContextResource

All Implemented Interfaces:
ContextResource, InputStreamSource, Resource

public class ServletContextResource extends AbstractFileResolvingResource implements ContextResource
Resource implementation for ServletContext resources, interpreting relative paths within the web application root directory.

Always supports stream access and URL access, but only allows java.io.File access when the web application archive is expanded.

Since:
28.12.2003
Author:
Juergen Hoeller
See Also:
  • ServletContext.getResourceAsStream(String)
  • ServletContext.getResource(String)
  • ServletContext.getRealPath(String)
  • Constructor Details

    • ServletContextResource

      public ServletContextResource(jakarta.servlet.ServletContext servletContext, String path)
      Create a new ServletContextResource for the given path.

      The Servlet spec requires that resource paths start with a slash, even if many containers accept paths without leading slash too. Consequently, the given path will be prepended with a slash if it doesn't already start with one.

      Parameters:
      servletContext - the ServletContext to load from
      path - the path of the resource
  • Method Details

    • getServletContext

      public final jakarta.servlet.ServletContext getServletContext()
      Return the ServletContext for this resource.
    • getPath

      public final String getPath()
      Return the path for this resource.
    • exists

      public boolean exists()
      This implementation checks ServletContext.getResource.
      Specified by:
      exists in interface Resource
      Overrides:
      exists in class AbstractFileResolvingResource
      See Also:
      • ServletContext.getResource(String)
    • isReadable

      public boolean isReadable()
      This implementation delegates to ServletContext.getResourceAsStream, which returns null in case of a non-readable resource (for example, a directory).
      Specified by:
      isReadable in interface Resource
      Overrides:
      isReadable in class AbstractFileResolvingResource
      See Also:
      • ServletContext.getResourceAsStream(String)
    • isFile

      public boolean isFile()
      Description copied from class: AbstractResource
      This implementation always returns false.
      Specified by:
      isFile in interface Resource
      Overrides:
      isFile in class AbstractFileResolvingResource
      See Also:
    • getInputStream

      public InputStream getInputStream() throws IOException
      This implementation delegates to ServletContext.getResourceAsStream, but throws a FileNotFoundException if no resource found.
      Specified by:
      getInputStream in interface InputStreamSource
      Returns:
      the input stream for the underlying resource (must not be null)
      Throws:
      IOException - if the content stream could not be opened
      See Also:
      • ServletContext.getResourceAsStream(String)
    • getURL

      public URL getURL() throws IOException
      This implementation delegates to ServletContext.getResource, but throws a FileNotFoundException if no resource found.
      Specified by:
      getURL in interface Resource
      Overrides:
      getURL in class AbstractResource
      Throws:
      IOException - if the resource cannot be resolved as URL, i.e. if the resource is not available as a descriptor
      See Also:
      • ServletContext.getResource(String)
    • getFile

      public File getFile() throws IOException
      This implementation resolves "file:" URLs or alternatively delegates to ServletContext.getRealPath, throwing a FileNotFoundException if not found or not resolvable.
      Specified by:
      getFile in interface Resource
      Overrides:
      getFile in class AbstractFileResolvingResource
      Throws:
      IOException - in case of general resolution/reading failures
      See Also:
      • ServletContext.getResource(String)
      • ServletContext.getRealPath(String)
    • createRelative

      public Resource createRelative(String relativePath)
      This implementation creates a ServletContextResource, applying the given path relative to the path of the underlying file of this resource descriptor.
      Specified by:
      createRelative in interface Resource
      Overrides:
      createRelative in class AbstractResource
      Parameters:
      relativePath - the relative path (relative to this resource)
      Returns:
      the resource handle for the relative resource
      See Also:
    • getFilename

      public @Nullable String getFilename()
      This implementation returns the name of the file that this ServletContext resource refers to.
      Specified by:
      getFilename in interface Resource
      Overrides:
      getFilename in class AbstractResource
      See Also:
    • getDescription

      public String getDescription()
      This implementation returns a description that includes the ServletContext resource location.
      Specified by:
      getDescription in interface Resource
      See Also:
    • getPathWithinContext

      public String getPathWithinContext()
      Description copied from interface: ContextResource
      Return the path within the enclosing 'context'.

      This is typically path relative to a context-specific root directory, for example, a ServletContext root or a PortletContext root.

      Specified by:
      getPathWithinContext in interface ContextResource
    • equals

      public boolean equals(@Nullable Object other)
      This implementation compares the underlying ServletContext resource locations.
      Overrides:
      equals in class AbstractResource
      See Also:
    • hashCode

      public int hashCode()
      This implementation returns the hash code of the underlying ServletContext resource location.
      Overrides:
      hashCode in class AbstractResource
      See Also: