Package org.springframework.web.context.support

# Class ServletContextResource

java.lang.Object
org.springframework.core.io.AbstractResource
org.springframework.core.io.AbstractFileResolvingResource
org.springframework.web.context.support.ServletContextResource

All Implemented Interfaces:
  `org.springframework.core.io.ContextResource`, `org.springframework.core.io.InputStreamSource`, `org.springframework.core.io.Resource`

public class ServletContextResource extends org.springframework.core.io.AbstractFileResolvingResource implements org.springframework.core.io.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(java.lang.String)`
  - `ServletContext.getResource(java.lang.String)`
  - `ServletContext.getRealPath(java.lang.String)`

## Constructor Summary

Constructors
Constructor
Description
`ServletContextResource(ServletContext servletContext, String path)`
Create a new `ServletContextResource` for the given path.

## Method Summary

Modifier and Type
Method
Description
`org.springframework.core.io.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.
`boolean`
`equals(Object other)`
This implementation compares the underlying ServletContext resource locations.
`boolean`
`exists()`
This implementation checks `ServletContext.getResource`.
`String`
`getDescription()`
This implementation returns a description that includes the ServletContext resource location.
`File`
`getFile()`
This implementation resolves "file:" URLs or alternatively delegates to `ServletContext.getRealPath`, throwing a FileNotFoundException if not found or not resolvable.
`String`
`getFilename()`
This implementation returns the name of the file that this ServletContext resource refers to.
`InputStream`
`getInputStream()`
This implementation delegates to `ServletContext.getResourceAsStream`, but throws a FileNotFoundException if no resource found.
`final String`
`getPath()`
Return the path for this resource.
`String`
`getPathWithinContext()`
`final ServletContext`
`getServletContext()`
Return the ServletContext for this resource.
`URL`
`getURL()`
This implementation delegates to `ServletContext.getResource`, but throws a FileNotFoundException if no resource found.
`int`
`hashCode()`
This implementation returns the hash code of the underlying ServletContext resource location.
`boolean`
`isFile()`
`boolean`
`isReadable()`
This implementation delegates to `ServletContext.getResourceAsStream`, which returns `null` in case of a non-readable resource (for example, a directory).

### Methods inherited from class org.springframework.core.io.AbstractFileResolvingResource

`contentLength, customizeConnection, customizeConnection, getFile, getFileForLastModifiedCheck, isFile, lastModified, readableChannel`

### Methods inherited from class org.springframework.core.io.AbstractResource

`getURI, isOpen, toString`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.springframework.core.io.Resource

`contentLength, getContentAsByteArray, getContentAsString, getURI, isOpen, lastModified, readableChannel`

## Constructor Details

### ServletContextResource

public ServletContextResource(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 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 `org.springframework.core.io.Resource`
Overrides:
  `exists` in class `org.springframework.core.io.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 `org.springframework.core.io.Resource`
Overrides:
  `isReadable` in class `org.springframework.core.io.AbstractFileResolvingResource`
See Also:
  - `ServletContext.getResourceAsStream(String)`

### isFile

public boolean isFile()

Specified by:
  `isFile` in interface `org.springframework.core.io.Resource`
Overrides:
  `isFile` in class `org.springframework.core.io.AbstractFileResolvingResource`

### 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 `org.springframework.core.io.InputStreamSource`
Throws:
  `IOException`
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 `org.springframework.core.io.Resource`
Overrides:
  `getURL` in class `org.springframework.core.io.AbstractResource`
Throws:
  `IOException`
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 `org.springframework.core.io.Resource`
Overrides:
  `getFile` in class `org.springframework.core.io.AbstractFileResolvingResource`
Throws:
  `IOException`
See Also:
  - `ServletContext.getResource(String)`
  - `ServletContext.getRealPath(String)`

### createRelative

public org.springframework.core.io.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 `org.springframework.core.io.Resource`
Overrides:
  `createRelative` in class `org.springframework.core.io.AbstractResource`
See Also:
  - `StringUtils.applyRelativePath(String, String)`

### getFilename

@Nullable public String getFilename()
This implementation returns the name of the file that this ServletContext resource refers to.

Specified by:
  `getFilename` in interface `org.springframework.core.io.Resource`
Overrides:
  `getFilename` in class `org.springframework.core.io.AbstractResource`
See Also:
  - `StringUtils.getFilename(String)`

### getDescription

public String getDescription()
This implementation returns a description that includes the ServletContext resource location.

Specified by:
  `getDescription` in interface `org.springframework.core.io.Resource`

### getPathWithinContext

public String getPathWithinContext()

Specified by:
  `getPathWithinContext` in interface `org.springframework.core.io.ContextResource`

### equals

public boolean equals(@Nullable Object other)
This implementation compares the underlying ServletContext resource locations.

Overrides:
  `equals` in class `org.springframework.core.io.AbstractResource`

### hashCode

public int hashCode()
This implementation returns the hash code of the underlying ServletContext resource location.

Overrides:
  `hashCode` in class `org.springframework.core.io.AbstractResource`