类 ServletUtils
- 从以下版本开始:
- 3.0
- 作者:
- TODAY 2020/12/8 23:07
-
字段概要
字段修饰符和类型字段说明static final StringDefault web app root key: "webapp.root".static final StringHTML escape parameter at the servlet context level (i.e. a context-param inweb.xml): "defaultHtmlEscape".static final StringUse of response encoding for HTML escaping parameter at the servlet context level (i.e. a context-param inweb.xml): "responseEncodedHtmlEscape".static final StringKey for the mutex session attribute.static final String[]Name suffixes in case of image buttons.static final StringStandard Servlet spec context attribute that specifies a temporary directory for the current web application, of typejava.io.File.static final StringWeb app root key parameter at the servlet context level (i.e. a context-param inweb.xml): "webAppRootKey".static final StringRequest attribute to hold the current web application context. -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static voidbindParts(HttpServletRequest request, cn.taketoday.beans.PropertyValues mpvs, boolean bindEmpty) Bind all parts from the given servlet request.static voidClear the Servlet spec's error attributes asHttpServletRequestattributes under the keys defined in the Servlet 2.3 specification:jakarta.servlet.error.status_code,jakarta.servlet.error.exception_type,jakarta.servlet.error.message,jakarta.servlet.error.exception,jakarta.servlet.error.request_uri,jakarta.servlet.error.servlet_name.static voidexposeErrorRequestAttributes(HttpServletRequest request, Throwable ex, String servletName) Expose the Servlet spec's error attributes asHttpServletRequestattributes under the keys defined in the Servlet 2.3 specification, for error pages that are rendered directly rather than through the Servlet container's error page resolution:jakarta.servlet.error.status_code,jakarta.servlet.error.exception_type,jakarta.servlet.error.message,jakarta.servlet.error.exception,jakarta.servlet.error.request_uri,jakarta.servlet.error.servlet_name.private static voidexposeRequestAttributeIfNotPresent(ServletRequest request, String name, Object value) Expose the specified request attribute if not already present.static StringfindParameterValue(ServletRequest request, String name) Obtain a named parameter from the given request parameters.static StringfindParameterValue(Map<String, ?> parameters, String name) Obtain a named parameter from the given request parameters.static WebApplicationContextfindWebApplicationContext(ServletRequest request) Look for the WebApplicationContext associated with the DispatcherServlet that has initiated request processing, and for the global context if none was found associated with the current request.static WebApplicationContextfindWebApplicationContext(ServletRequest request, ServletContext servletContext) Look for the WebApplicationContext associated with the DispatcherServlet that has initiated request processing, and for the global context if none was found associated with the current request.static CookiegetCookie(HttpServletRequest request, String name) Retrieve the first cookie with the given name.static BooleangetDefaultHtmlEscape(ServletContext servletContext) Return whether default HTML escaping is enabled for the web application, i.e. the value of the "defaultHtmlEscape" context-param inweb.xml(if any).static HttpSessiongetHttpSession(RequestContext context) Get HttpSessionstatic HttpSessiongetHttpSession(RequestContext context, boolean create) Returns the currentHttpSessionassociated with this request or, if there is no current session andcreateis true, returns a new session.static <T> TgetNativeRequest(ServletRequest request, Class<T> requiredType) Return an appropriate request object of the specified type, if available, unwrapping the given request as far as necessary.static <T> TgetNativeResponse(ServletResponse response, Class<T> requiredType) Return an appropriate response object of the specified type, if available, unwrapping the given response as far as necessary.getParametersStartingWith(ServletRequest request, String prefix) Return a map containing all parameters with the given prefix.static PartgetPart(RequestContext request, String requestPartName) static PartgetPart(HttpServletRequest request, String requestPartName) getParts(HttpServletRequest request) Retrieve all parts from the given servlet request.getParts(HttpServletRequest request, String name) Retrieve all parts with the given name from the given servlet request.static StringgetRealPath(ServletContext servletContext, String path) Return the real path of the given path within the web application, as provided by the servlet container.static RequestContextgetRequestContext(WebApplicationContext webApplicationContext, HttpServletRequest request, HttpServletResponse response) static RequestContextgetRequestContext(ServletRequest request, ServletResponse response) static ObjectgetRequiredSessionAttribute(HttpServletRequest request, String name) Check the given request for a session attribute of the given name.static BooleangetResponseEncodedHtmlEscape(ServletContext servletContext) Return whether response encoding should be used when HTML escaping characters, thus only escaping XML markup significant characters with UTF-* encodings.static ServletContextgetServletContext(RequestContext context) Gets the servlet context to which this ServletRequest was last dispatched.static HttpServletRequestgetServletRequest(RequestContext context) Return an appropriate HttpServletRequest objectstatic HttpServletResponsegetServletResponse(RequestContext context) Return an appropriate response objectstatic ObjectgetSessionAttribute(HttpServletRequest request, String name) Check the given request for a session attribute of the given name.static StringgetSessionId(HttpServletRequest request) Determine the session id of the given request, if any.static ObjectgetSessionMutex(HttpSession session) Return the best available mutex for the given session: that is, an object to synchronize on for the given session.static FilegetTempDir(ServletContext servletContext) Return the temporary directory for the current web application, as provided by the servlet container.static booleanhasSubmitParameter(ServletRequest request, String name) Check if a specific input type="submit" parameter was sent in the request, either via a button (directly with name) or via an image (name + ".x" or name + ".y").static booleanisIncludeRequest(ServletRequest request) Determine whether the given request is an include request, that is, not a top-level HTTP request coming in from the outside.static booleanisPostForm(HttpServletRequest request) static voidremoveWebAppRootSystemProperty(ServletContext servletContext) Remove the system property that points to the web app root directory.static voidsetSessionAttribute(HttpServletRequest request, String name, Object value) Set the session attribute with the given name to the given value.static voidsetWebAppRootSystemProperty(ServletContext servletContext) Set a system property to the web application root directory.
-
字段详细资料
-
SESSION_MUTEX_ATTRIBUTE
Key for the mutex session attribute. -
WEB_APPLICATION_CONTEXT_ATTRIBUTE
Request attribute to hold the current web application context. Otherwise only the global web app context is obtainable by tags etc. -
SUBMIT_IMAGE_SUFFIXES
Name suffixes in case of image buttons. @since 4.0 -
TEMP_DIR_CONTEXT_ATTRIBUTE
Standard Servlet spec context attribute that specifies a temporary directory for the current web application, of typejava.io.File.- 从以下版本开始:
- 4.0
- 另请参阅:
-
HTML_ESCAPE_CONTEXT_PARAM
HTML escape parameter at the servlet context level (i.e. a context-param inweb.xml): "defaultHtmlEscape".- 从以下版本开始:
- 4.0
- 另请参阅:
-
RESPONSE_ENCODED_HTML_ESCAPE_CONTEXT_PARAM
Use of response encoding for HTML escaping parameter at the servlet context level (i.e. a context-param inweb.xml): "responseEncodedHtmlEscape".- 从以下版本开始:
- 4.0
- 另请参阅:
-
WEB_APP_ROOT_KEY_PARAM
Web app root key parameter at the servlet context level (i.e. a context-param inweb.xml): "webAppRootKey".- 从以下版本开始:
- 4.0
- 另请参阅:
-
DEFAULT_WEB_APP_ROOT_KEY
Default web app root key: "webapp.root".- 从以下版本开始:
- 4.0
- 另请参阅:
-
-
构造器详细资料
-
ServletUtils
public ServletUtils()
-
-
方法详细资料
-
getRequestContext
-
getRequestContext
public static RequestContext getRequestContext(WebApplicationContext webApplicationContext, HttpServletRequest request, HttpServletResponse response) -
getHttpSession
Get HttpSession- 抛出:
IllegalStateException- Not run in servlet
-
getHttpSession
Returns the currentHttpSessionassociated with this request or, if there is no current session andcreateis true, returns a new session.If
createisfalseand the request has no validHttpSession, this method returnsnull.To make sure the session is properly maintained, you must call this method before the response is committed. If the container is using cookies to maintain session integrity and is asked to create a new session when the response is committed, an IllegalStateException is thrown.
- 参数:
create-trueto create a new session for this request if necessary;falseto returnnullif there's no current session- 返回:
- the
HttpSessionassociated with this request ornullifcreateisfalseand the request has no valid session - 抛出:
IllegalStateException- Not run in servlet- 另请参阅:
-
getNativeRequest
@Nullable public static <T> T getNativeRequest(ServletRequest request, @Nullable Class<T> requiredType) Return an appropriate request object of the specified type, if available, unwrapping the given request as far as necessary.- 参数:
request- the servlet request to introspectrequiredType- the desired type of request object- 返回:
- the matching request object, or
nullif none of that type is available
-
getNativeResponse
@Nullable public static <T> T getNativeResponse(ServletResponse response, @Nullable Class<T> requiredType) Return an appropriate response object of the specified type, if available, unwrapping the given response as far as necessary.- 参数:
response- the servlet response to introspectrequiredType- the desired type of response object- 返回:
- the matching response object, or
nullif none of that type is available
-
getServletRequest
Return an appropriate HttpServletRequest object- 参数:
context- the context to introspect- 返回:
- the matching request object
- 另请参阅:
-
getServletContext
Gets the servlet context to which this ServletRequest was last dispatched.- 返回:
- the servlet context to which this ServletRequest was last dispatched
- 从以下版本开始:
- 4.0
-
getServletResponse
Return an appropriate response object- 参数:
context- the context to introspect- 返回:
- the matching response object
- 另请参阅:
-
findWebApplicationContext
Look for the WebApplicationContext associated with the DispatcherServlet that has initiated request processing, and for the global context if none was found associated with the current request. The global context will be found via the ServletContext or via ContextLoader's current context.- 参数:
request- current HTTP request- 返回:
- the request-specific WebApplicationContext, or the global one
if no request-specific context has been found, or
nullif none - 从以下版本开始:
- 4.0
- 另请参阅:
-
findWebApplicationContext
@Nullable public static WebApplicationContext findWebApplicationContext(ServletRequest request, @Nullable ServletContext servletContext) Look for the WebApplicationContext associated with the DispatcherServlet that has initiated request processing, and for the global context if none was found associated with the current request. The global context will be found via the ServletContext or via ContextLoader's current context.NOTE: This variant remains compatible with Servlet 2.5, explicitly checking a given ServletContext instead of deriving it from the request.
- 参数:
request- current HTTP requestservletContext- current servlet context- 返回:
- the request-specific WebApplicationContext, or the global one
if no request-specific context has been found, or
nullif none - 从以下版本开始:
- 4.0
- 另请参阅:
-
getSessionId
Determine the session id of the given request, if any.- 参数:
request- current HTTP request- 返回:
- the session id, or
nullif none - 从以下版本开始:
- 4.0
-
getSessionAttribute
Check the given request for a session attribute of the given name. Returns null if there is no session or if the session has no such attribute. Does not create a new session if none has existed before!- 参数:
request- current HTTP requestname- the name of the session attribute- 返回:
- the value of the session attribute, or
nullif not found - 从以下版本开始:
- 4.0
-
getRequiredSessionAttribute
Check the given request for a session attribute of the given name. Throws an exception if there is no session or if the session has no such attribute. Does not create a new session if none has existed before!- 参数:
request- current HTTP requestname- the name of the session attribute- 返回:
- the value of the session attribute, or
nullif not found - 抛出:
IllegalStateException- if the session attribute could not be found- 从以下版本开始:
- 4.0
-
setSessionAttribute
public static void setSessionAttribute(HttpServletRequest request, String name, @Nullable Object value) Set the session attribute with the given name to the given value. Removes the session attribute if value is null, if a session existed at all. Does not create a new session if not necessary!- 参数:
request- current HTTP requestname- the name of the session attributevalue- the value of the session attribute- 从以下版本开始:
- 4.0
-
getSessionMutex
Return the best available mutex for the given session: that is, an object to synchronize on for the given session.Returns the session mutex attribute if available; usually, this means that the HttpSessionMutexListener needs to be defined in
web.xml. Falls back to the HttpSession itself if no mutex attribute found.The session mutex is guaranteed to be the same object during the entire lifetime of the session, available under the key defined by the
SESSION_MUTEX_ATTRIBUTEconstant. It serves as a safe reference to synchronize on for locking on the current session.In many cases, the HttpSession reference itself is a safe mutex as well, since it will always be the same object reference for the same active logical session. However, this is not guaranteed across different servlet containers; the only 100% safe way is a session mutex.
- 参数:
session- the HttpSession to find a mutex for- 返回:
- the mutex object (never
null) - 从以下版本开始:
- 4.0
- 另请参阅:
-
hasSubmitParameter
Check if a specific input type="submit" parameter was sent in the request, either via a button (directly with name) or via an image (name + ".x" or name + ".y").- 参数:
request- current HTTP requestname- the name of the parameter- 返回:
- if the parameter was sent
- 从以下版本开始:
- 4.0
- 另请参阅:
-
isIncludeRequest
Determine whether the given request is an include request, that is, not a top-level HTTP request coming in from the outside.Checks the presence of the "jakarta.servlet.include.request_uri" request attribute. Could check any request attribute that is only present in an include request.
- 参数:
request- current servlet request- 返回:
- whether the given request is an include request
- 从以下版本开始:
- 4.0
-
isPostForm
- 从以下版本开始:
- 4.0
-
getCookie
Retrieve the first cookie with the given name. Note that multiple cookies can have the same name but different paths or domains.- 参数:
request- current servlet requestname- cookie name- 返回:
- the first cookie with the given name, or
nullif none is found - 从以下版本开始:
- 4.0
-
getParametersStartingWith
public static Map<String,Object> getParametersStartingWith(ServletRequest request, @Nullable String prefix) Return a map containing all parameters with the given prefix. Maps single values to String and multiple values to String array.For example, with a prefix of "spring_", "spring_param1" and "spring_param2" result in a Map with "param1" and "param2" as keys.
- 参数:
request- the HTTP request in which to look for parametersprefix- the beginning of parameter names (if this is null or the empty string, all parameters will match)- 返回:
- map containing request parameters without the prefix, containing either a String or a String array as values
- 另请参阅:
-
findParameterValue
Obtain a named parameter from the given request parameters.See
findParameterValue(Map, String)for a description of the lookup algorithm.- 参数:
request- current HTTP requestname- the logical name of the request parameter- 返回:
- the value of the parameter, or
nullif the parameter does not exist in given request
-
findParameterValue
Obtain a named parameter from the given request parameters.This method will try to obtain a parameter value using the following algorithm:
- Try to get the parameter value using just the given logical name. This handles parameters of the form logicalName = value. For normal parameters, e.g. submitted using a hidden HTML form field, this will return the requested value.
- Try to obtain the parameter value from the parameter name, where the parameter name in the request is of the form logicalName_value = xyz with "_" being the configured delimiter. This deals with parameter values submitted using an HTML form submit button.
- If the value obtained in the previous step has a ".x" or ".y" suffix, remove that. This handles cases where the value was submitted using an HTML form image button. In this case the parameter in the request would actually be of the form logicalName_value.x = 123.
- 参数:
parameters- the available parameter mapname- the logical name of the request parameter- 返回:
- the value of the parameter, or
nullif the parameter does not exist in given request
-
exposeErrorRequestAttributes
public static void exposeErrorRequestAttributes(HttpServletRequest request, Throwable ex, @Nullable String servletName) Expose the Servlet spec's error attributes asHttpServletRequestattributes under the keys defined in the Servlet 2.3 specification, for error pages that are rendered directly rather than through the Servlet container's error page resolution:jakarta.servlet.error.status_code,jakarta.servlet.error.exception_type,jakarta.servlet.error.message,jakarta.servlet.error.exception,jakarta.servlet.error.request_uri,jakarta.servlet.error.servlet_name.Does not override values if already present, to respect attribute values that have been exposed explicitly before.
Exposes status code 200 by default. Set the "jakarta.servlet.error.status_code" attribute explicitly (before or after) in order to expose a different status code.
- 参数:
request- current servlet requestex- the exception encounteredservletName- the name of the offending servlet- 从以下版本开始:
- 4.0
-
exposeRequestAttributeIfNotPresent
private static void exposeRequestAttributeIfNotPresent(ServletRequest request, String name, Object value) Expose the specified request attribute if not already present.- 参数:
request- current servlet requestname- the name of the attributevalue- the suggested value of the attribute- 从以下版本开始:
- 4.0
-
clearErrorRequestAttributes
Clear the Servlet spec's error attributes asHttpServletRequestattributes under the keys defined in the Servlet 2.3 specification:jakarta.servlet.error.status_code,jakarta.servlet.error.exception_type,jakarta.servlet.error.message,jakarta.servlet.error.exception,jakarta.servlet.error.request_uri,jakarta.servlet.error.servlet_name.- 参数:
request- current servlet request- 从以下版本开始:
- 4.0
-
setWebAppRootSystemProperty
public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException Set a system property to the web application root directory. The key of the system property can be defined with the "webAppRootKey" context-param inweb.xml. Default is "webapp.root".Can be used for tools that support substitution with
System.getPropertyvalues, like log4j's "${key}" syntax within log file locations.- 参数:
servletContext- the servlet context of the web application- 抛出:
IllegalStateException- if the system property is already set, or if the WAR file is not expanded- 另请参阅:
-
removeWebAppRootSystemProperty
Remove the system property that points to the web app root directory. To be called on shutdown of the web application.- 参数:
servletContext- the servlet context of the web application- 另请参阅:
-
getDefaultHtmlEscape
Return whether default HTML escaping is enabled for the web application, i.e. the value of the "defaultHtmlEscape" context-param inweb.xml(if any).This method differentiates between no param specified at all and an actual boolean value specified, allowing to have a context-specific default in case of no setting at the global level.
- 参数:
servletContext- the servlet context of the web application- 返回:
- whether default HTML escaping is enabled for the given application
(
null= no explicit default)
-
getResponseEncodedHtmlEscape
@Nullable public static Boolean getResponseEncodedHtmlEscape(@Nullable ServletContext servletContext) Return whether response encoding should be used when HTML escaping characters, thus only escaping XML markup significant characters with UTF-* encodings. This option is enabled for the web application with a ServletContext param, i.e. the value of the "responseEncodedHtmlEscape" context-param inweb.xml(if any).This method differentiates between no param specified at all and an actual boolean value specified, allowing to have a context-specific default in case of no setting at the global level.
- 参数:
servletContext- the servlet context of the web application- 返回:
- whether response encoding is to be used for HTML escaping
(
null= no explicit default) - 从以下版本开始:
- 4.0
-
getTempDir
Return the temporary directory for the current web application, as provided by the servlet container.- 参数:
servletContext- the servlet context of the web application- 返回:
- the File representing the temporary directory
-
getRealPath
public static String getRealPath(ServletContext servletContext, String path) throws FileNotFoundException Return the real path of the given path within the web application, as provided by the servlet container.Prepends a slash if the path does not already start with a slash, and throws a FileNotFoundException if the path cannot be resolved to a resource (in contrast to ServletContext's
getRealPath, which returns null).- 参数:
servletContext- the servlet context of the web applicationpath- the path within the web application- 返回:
- the corresponding real path
- 抛出:
FileNotFoundException- if the path cannot be resolved to a resource- 另请参阅:
-
getParts
public static cn.taketoday.util.MultiValueMap<String,Part> getParts(HttpServletRequest request) throws MultipartException Retrieve all parts from the given servlet request.- 参数:
request- the servlet request- 返回:
- the parts in a MultiValueMap
- 抛出:
MultipartException- in case of failures- 从以下版本开始:
- 4.0
-
getParts
public static List<Part> getParts(HttpServletRequest request, String name) throws MultipartException Retrieve all parts with the given name from the given servlet request.- 参数:
request- the servlet requestname- the name to look for- 返回:
- the parts in a MultiValueMap
- 抛出:
MultipartException- in case of failures- 从以下版本开始:
- 4.0
-
getPart
-
getPart
-
bindParts
public static void bindParts(HttpServletRequest request, cn.taketoday.beans.PropertyValues mpvs, boolean bindEmpty) throws MultipartException Bind all parts from the given servlet request.- 参数:
request- the servlet requestmpvs- the property values to bind tobindEmpty- whether to bind empty parts as well- 抛出:
MultipartException- in case of failures- 从以下版本开始:
- 4.0
-