Top Description Fields Constructors Methods
org.springframework.web.context.support

public Class ServletContextResource

extends AbstractFileResolvingResource
implements ContextResource
Class Inheritance
All Implemented Interfaces
org.springframework.core.io.ContextResource, org.springframework.core.io.Resource, org.springframework.core.io.InputStreamSource
Imports
java.io.File, .FileNotFoundException, .IOException, .InputStream, java.net.MalformedURLException, .URL, jakarta.servlet.ServletContext, org.springframework.core.io.AbstractFileResolvingResource, .ContextResource, .Resource, org.springframework.lang.Nullable, org.springframework.util.Assert, .ResourceUtils, .StringUtils, org.springframework.web.util.WebUtils

org.springframework.core.io.Resource implementation for jakarta.servlet.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.

Author
Juergen Hoeller
Since
28.12.2003
See Also
jakarta.servlet.ServletContext#getResourceAsStream, jakarta.servlet.ServletContext#getResource, jakarta.servlet.ServletContext#getRealPath

Field Summary

Modifier and TypeField and Description
private final String
private final ServletContext

Constructor Summary

AccessConstructor and Description
public
ServletContextResource(ServletContext
the ServletContext to load from
servletContext
,
String
the path of the resource
path
)

Create a new ServletContextResource for the given path.

Method Summary

Modifier and TypeMethod and Description
public Resource
createRelative(String
the relative path (relative to this resource)
relativePath
)

Overrides org.springframework.core.io.AbstractResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

This implementation creates a ServletContextResource, applying the given path relative to the path of the underlying file of this resource descriptor.
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides org.springframework.core.io.AbstractResource.equals.

This implementation compares the underlying ServletContext resource locations.
public boolean
exists()

Overrides org.springframework.core.io.AbstractFileResolvingResource.exists.

Implements org.springframework.core.io.Resource.exists.

This implementation checks ServletContext.getResource.
public String
getDescription()

Implements org.springframework.core.io.Resource.getDescription.

This implementation returns a description that includes the ServletContext resource location.
public File
getFile()

Overrides org.springframework.core.io.AbstractFileResolvingResource.getFile.

Implements org.springframework.core.io.Resource.getFile.

This implementation resolves "file:" URLs or alternatively delegates to ServletContext.getRealPath, throwing a FileNotFoundException if not found or not resolvable.
public String
getFilename()

Overrides org.springframework.core.io.AbstractResource.getFilename.

Implements org.springframework.core.io.Resource.getFilename.

This implementation returns the name of the file that this ServletContext resource refers to.
public InputStream
getInputStream()

Implements org.springframework.core.io.InputStreamSource.getInputStream.

This implementation delegates to ServletContext.getResourceAsStream, but throws a FileNotFoundException if no resource found.
public final String
getPath()

Return the path for this resource.

public String
getPathWithinContext()

Implements org.springframework.core.io.ContextResource.getPathWithinContext.

Return the path within the enclosing 'context'.
public final ServletContext
getServletContext()

Return the ServletContext for this resource.

public URL
getURL()

Overrides org.springframework.core.io.AbstractResource.getURL.

Implements org.springframework.core.io.Resource.getURL.

This implementation delegates to ServletContext.getResource, but throws a FileNotFoundException if no resource found.
public int
hashCode()

Overrides org.springframework.core.io.AbstractResource.hashCode.

This implementation returns the hash code of the underlying ServletContext resource location.
public boolean
isFile()

Overrides org.springframework.core.io.AbstractFileResolvingResource.isFile.

Overrides default org.springframework.core.io.Resource.isFile.

Determine whether this resource represents a file in a file system.
public boolean
isReadable()

Overrides org.springframework.core.io.AbstractFileResolvingResource.isReadable.

Overrides default org.springframework.core.io.Resource.isReadable.

This implementation delegates to ServletContext.getResourceAsStream, which returns null in case of a non-readable resource (e.g. a directory).
Inherited from org.springframework.core.io.AbstractFileResolvingResource:
contentLengthcustomizeConnectioncustomizeConnectiongetFilegetFileForLastModifiedCheckisFilelastModifiedreadableChannel

Field Detail

pathback to summary
private final String path
servletContextback to summary
private final ServletContext servletContext

Constructor Detail

ServletContextResourceback to summary
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:ServletContext

the ServletContext to load from

path:String

the path of the resource

Method Detail

createRelativeback to summary
public Resource createRelative(String relativePath)

Overrides org.springframework.core.io.AbstractResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

This implementation creates a ServletContextResource, applying the given path relative to the path of the underlying file of this resource descriptor.

Parameters
relativePath:String

Doc from org.springframework.core.io.Resource.createRelative.

the relative path (relative to this resource)

Returns:Resource

Doc from org.springframework.core.io.Resource.createRelative.

the resource handle for the relative resource

Annotations
@Override
See Also
org.springframework.util.StringUtils#applyRelativePath(String, String)
equalsback to summary
public boolean equals(Object other)

Overrides org.springframework.core.io.AbstractResource.equals.

This implementation compares the underlying ServletContext resource locations.

Parameters
other:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
existsback to summary
public boolean exists()

Overrides org.springframework.core.io.AbstractFileResolvingResource.exists.

Implements org.springframework.core.io.Resource.exists.

This implementation checks ServletContext.getResource.

Annotations
@Override
See Also
jakarta.servlet.ServletContext#getResource(String)
getDescriptionback to summary
public String getDescription()

Implements org.springframework.core.io.Resource.getDescription.

This implementation returns a description that includes the ServletContext resource location.

Annotations
@Override
getFileback to summary
public File getFile() throws IOException

Overrides org.springframework.core.io.AbstractFileResolvingResource.getFile.

Implements org.springframework.core.io.Resource.getFile.

This implementation resolves "file:" URLs or alternatively delegates to ServletContext.getRealPath, throwing a FileNotFoundException if not found or not resolvable.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.getFile.

in case of general resolution/reading failures

See Also
jakarta.servlet.ServletContext#getResource(String), jakarta.servlet.ServletContext#getRealPath(String)
getFilenameback to summary
public String getFilename()

Overrides org.springframework.core.io.AbstractResource.getFilename.

Implements org.springframework.core.io.Resource.getFilename.

This implementation returns the name of the file that this ServletContext resource refers to.

Annotations
@Override
@Nullable
See Also
org.springframework.util.StringUtils#getFilename(String)
getInputStreamback to summary
public InputStream getInputStream() throws IOException

Implements org.springframework.core.io.InputStreamSource.getInputStream.

This implementation delegates to ServletContext.getResourceAsStream, but throws a FileNotFoundException if no resource found.

Returns:InputStream

Doc from org.springframework.core.io.InputStreamSource.getInputStream.

the input stream for the underlying resource (must not be null)

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.InputStreamSource.getInputStream.

if the content stream could not be opened

See Also
jakarta.servlet.ServletContext#getResourceAsStream(String)
getPathback to summary
public final String getPath()

Return the path for this resource.

getPathWithinContextback to summary
public String getPathWithinContext()

Implements org.springframework.core.io.ContextResource.getPathWithinContext.

Doc from org.springframework.core.io.ContextResource.getPathWithinContext.

Return the path within the enclosing 'context'.

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

Annotations
@Override
getServletContextback to summary
public final ServletContext getServletContext()

Return the ServletContext for this resource.

getURLback to summary
public URL getURL() throws IOException

Overrides org.springframework.core.io.AbstractResource.getURL.

Implements org.springframework.core.io.Resource.getURL.

This implementation delegates to ServletContext.getResource, but throws a FileNotFoundException if no resource found.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.getURL.

if the resource cannot be resolved as URL, i.e. if the resource is not available as a descriptor

See Also
jakarta.servlet.ServletContext#getResource(String)
hashCodeback to summary
public int hashCode()

Overrides org.springframework.core.io.AbstractResource.hashCode.

This implementation returns the hash code of the underlying ServletContext resource location.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

Annotations
@Override
isFileback to summary
public boolean isFile()

Overrides org.springframework.core.io.AbstractFileResolvingResource.isFile.

Overrides default org.springframework.core.io.Resource.isFile.

Doc from org.springframework.core.io.Resource.isFile.

Determine whether this resource represents a file in a file system.

A value of true strongly suggests (but does not guarantee) that a getFile() call will succeed.

This is conservatively false by default.

Annotations
@Override
isReadableback to summary
public boolean isReadable()

Overrides org.springframework.core.io.AbstractFileResolvingResource.isReadable.

Overrides default org.springframework.core.io.Resource.isReadable.

This implementation delegates to ServletContext.getResourceAsStream, which returns null in case of a non-readable resource (e.g. a directory).

Annotations
@Override
See Also
jakarta.servlet.ServletContext#getResourceAsStream(String)