类 ResourceServlet
- java.lang.Object
- javax.servlet.GenericServlet
- javax.servlet.http.HttpServlet
- org.springframework.web.servlet.HttpServletBean
- org.springframework.web.servlet.ResourceServlet
- 所有已实现的接口:
Serializable
,Servlet
,ServletConfig
,Aware
,EnvironmentAware
,EnvironmentCapable
@Deprecated public class ResourceServlet extends HttpServletBean
已过时。as of Spring 4.3.5, in favor ofResourceHttpRequestHandler
Simple servlet that can expose an internal resource, including a default URL if the specified resource is not found. An alternative, for example, to trying and catching exceptions when using JSP include.A further usage of this servlet is the ability to apply last-modified timestamps to quasi-static resources (typically JSPs). This can happen as bridge to parameter-specified resources, or as proxy for a specific target resource (or a list of specific target resources to combine).
A typical usage would map a URL like "/ResourceServlet" onto an instance of this servlet, and use the "JSP include" action to include this URL, with the "resource" parameter indicating the actual target path in the WAR.
The
defaultUrl
property can be set to the internal resource path of a default URL, to be rendered when the target resource is not found or not specified in the first place.The "resource" parameter and the
defaultUrl
property can also specify a list of target resources to combine. Those resources will be included one by one to build the response. If last-modified determination is active, the newest timestamp among those files will be used.The
allowedResources
property can be set to a URL pattern of resources that should be available via this servlet. If not set, any target resource can be requested, including resources in the WEB-INF directory!If using this servlet for direct access rather than via includes, the
contentType
property should be specified to apply a proper content type. Note that a content type header in the target JSP will be ignored when including the resource via a RequestDispatcher include.To apply last-modified timestamps for the target resource, set the
applyLastModified
property to true. This servlet will then return the file timestamp of the target resource as last-modified value, falling back to the startup time of this servlet if not retrievable.Note that applying the last-modified timestamp in the above fashion just makes sense if the target resource does not generate content that depends on the HttpSession or cookies; it is just allowed to evaluate request parameters.
A typical case for such last-modified usage is a JSP that just makes minimal usage of basic means like includes or message resolution to build quasi-static content. Regenerating such content on every request is unnecessary; it can be cached as long as the file hasn't changed.
Note that this servlet will apply the last-modified timestamp if you tell it to do so: It's your decision whether the content of the target resource can be cached in such a fashion. Typical use cases are helper resources that are not fronted by a controller, like JavaScript files that are generated by a JSP (without depending on the HttpSession).
- 作者:
- Juergen Hoeller, Rod Johnson
- 另请参阅:
setDefaultUrl(java.lang.String)
,setAllowedResources(java.lang.String)
,setApplyLastModified(boolean)
, 序列化表格
字段概要
字段 修饰符和类型 字段 说明 static String
RESOURCE_PARAM_NAME
已过时。Name of the parameter that must contain the actual resource path.static String
RESOURCE_URL_DELIMITERS
已过时。Any number of these characters are considered delimiters between multiple resource paths in a single String value.从类继承的字段 org.springframework.web.servlet.HttpServletBean
logger
构造器概要
构造器 构造器 说明 ResourceServlet()
已过时。
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 protected String
determineResourceUrl(HttpServletRequest request)
已过时。Determine the URL of the target resource of this request.protected void
doGet(HttpServletRequest request, HttpServletResponse response)
已过时。Determine the URL of the target resource and include it.protected long
getFileTimestamp(String resourceUrl)
已过时。Return the file timestamp for the given resource.protected long
getLastModified(HttpServletRequest request)
已过时。Return the last-modified timestamp of the file that corresponds to the target resource URL (i.e. typically the request ".jsp" file).protected PathMatcher
getPathMatcher()
已过时。Return aPathMatcher
to use for matching the "allowedResources" URL pattern.protected void
initServletBean()
已过时。Remember the startup time, using no last-modified time before it.void
setAllowedResources(String allowedResources)
已过时。Set allowed resources as URL pattern, e.g.void
setApplyLastModified(boolean applyLastModified)
已过时。Set whether to apply the file timestamp of the target resource as last-modified value.void
setContentType(String contentType)
已过时。Set the content type of the target resource (typically a JSP).void
setDefaultUrl(String defaultUrl)
已过时。Set the URL within the current web application from which to include content if the requested path isn't found, or if none is specified in the first place.从类继承的方法 org.springframework.web.servlet.HttpServletBean
addRequiredProperty, createEnvironment, getEnvironment, getServletContext, getServletName, init, initBeanWrapper, setEnvironment
从类继承的方法 javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, service, service
从类继承的方法 javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletInfo, init, log, log
字段详细资料
RESOURCE_URL_DELIMITERS
public static final String RESOURCE_URL_DELIMITERS
已过时。Any number of these characters are considered delimiters between multiple resource paths in a single String value.- 另请参阅:
- 常量字段值
RESOURCE_PARAM_NAME
public static final String RESOURCE_PARAM_NAME
已过时。Name of the parameter that must contain the actual resource path.- 另请参阅:
- 常量字段值
构造器详细资料
ResourceServlet
public ResourceServlet()
已过时。
方法详细资料
setDefaultUrl
public void setDefaultUrl(String defaultUrl)
已过时。Set the URL within the current web application from which to include content if the requested path isn't found, or if none is specified in the first place.If specifying multiple URLs, they will be included one by one to build the response. If last-modified determination is active, the newest timestamp among those files will be used.
setAllowedResources
public void setAllowedResources(String allowedResources)
已过时。Set allowed resources as URL pattern, e.g. "/WEB-INF/res/*.jsp", The parameter can be any Ant-style pattern parsable by AntPathMatcher.- 另请参阅:
AntPathMatcher
setContentType
public void setContentType(String contentType)
已过时。Set the content type of the target resource (typically a JSP). Default is none, which is appropriate when including resources.For directly accessing resources, for example to leverage this servlet's last-modified support, specify a content type here. Note that a content type header in the target JSP will be ignored when including the resource via a RequestDispatcher include.
setApplyLastModified
public void setApplyLastModified(boolean applyLastModified)
已过时。Set whether to apply the file timestamp of the target resource as last-modified value. Default is "false".This is mainly intended for JSP targets that don't generate session-specific or database-driven content: Such files can be cached by the browser as long as the last-modified timestamp of the JSP file doesn't change.
This will only work correctly with expanded WAR files that allow access to the file timestamps. Else, the startup time of this servlet is returned.
initServletBean
protected void initServletBean()
已过时。Remember the startup time, using no last-modified time before it.- 覆盖:
initServletBean
在类中HttpServletBean
getPathMatcher
protected PathMatcher getPathMatcher()
已过时。Return aPathMatcher
to use for matching the "allowedResources" URL pattern.The default is
AntPathMatcher
.
doGet
protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
已过时。Determine the URL of the target resource and include it.
determineResourceUrl
protected String determineResourceUrl(HttpServletRequest request)
已过时。Determine the URL of the target resource of this request.Default implementation returns the value of the "resource" parameter. Can be overridden in subclasses.
- 参数:
request
- current HTTP request- 返回:
- the URL of the target resource, or
null
if none found - 另请参阅:
RESOURCE_PARAM_NAME
getLastModified
protected final long getLastModified(HttpServletRequest request)
已过时。Return the last-modified timestamp of the file that corresponds to the target resource URL (i.e. typically the request ".jsp" file). Will simply return -1 if "applyLastModified" is false (the default).Returns no last-modified date before the startup time of this servlet, to allow for message resolution etc that influences JSP contents, assuming that those background resources might have changed on restart.
Returns the startup time of this servlet if the file that corresponds to the target resource URL couldn't be resolved (for example, because the WAR is not expanded).
getFileTimestamp
protected long getFileTimestamp(String resourceUrl)
已过时。Return the file timestamp for the given resource.- 参数:
resourceUrl
- the URL of the resource- 返回:
- the file timestamp in milliseconds, or -1 if not determinable