Class UrlPathHelper
RequestDispatcher includes and support for consistent URL decoding.
 Used by AbstractUrlHandlerMapping
 and RequestContext for path matching
 and/or URI determination.
- Since:
- 14.01.2004
- Author:
- Juergen Hoeller, Rob Harrop, Rossen Stoyanchev
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final UrlPathHelperShared, read-only instance with defaults.static final StringName of Servlet request attribute that holds aresolvedlookupPath.static final UrlPathHelperShared, read-only instance for the full, encoded path.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptiondecodeMatrixVariables(HttpServletRequest request, MultiValueMap<String, String> vars) Decode the given matrix variables viadecodeRequestString(jakarta.servlet.http.HttpServletRequest, java.lang.String)unlesssetUrlDecode(boolean)is set totruein which case it is assumed the URL path from which the variables were extracted is already decoded through a call togetLookupPathForRequest(HttpServletRequest).decodePathVariables(HttpServletRequest request, Map<String, String> vars) Decode the given URI path variables viadecodeRequestString(jakarta.servlet.http.HttpServletRequest, java.lang.String)unlesssetUrlDecode(boolean)is set totruein which case it is assumed the URL path from which the variables were extracted is already decoded through a call togetLookupPathForRequest(HttpServletRequest).decodeRequestString(HttpServletRequest request, String source) Decode the given source string with a URLDecoder.protected StringdetermineEncoding(HttpServletRequest request) Determine the encoding for the given request.getContextPath(HttpServletRequest request) Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.protected StringReturn the default character encoding to use for URL decoding.Return the mapping lookup path for the given request, within the current servlet mapping if applicable, else within the web application.Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.Return the query string part of the given request's URL.Return the request URI for the given request.Return the servlet path for the given request, detecting an include request URL if called within a RequestDispatcher include.Return the path within the web application for the given request.Return the path within the servlet mapping for the given request, i.e.protected StringgetPathWithinServletMapping(HttpServletRequest request, String pathWithinApp) Return the path within the servlet mapping for the given request, i.e.getRequestUri(HttpServletRequest request) Return the request URI for the given request, detecting an include request URL if called within a RequestDispatcher include.static StringgetResolvedLookupPath(ServletRequest request) Return a previouslyresolvedlookupPath.getServletPath(HttpServletRequest request) Return the servlet path for the given request, regarding an include request URL if called within a RequestDispatcher include.booleanWhether to decode the request URI when determining the lookup path.removeSemicolonContent(String requestUri) Remove ";" (semicolon) content from the given request URI if the removeSemicolonContent property is set to "true".Resolvethe lookupPath and cache it in a request attribute with the keyPATH_ATTRIBUTEfor subsequent access viagetResolvedLookupPath(ServletRequest).voidsetAlwaysUseFullPath(boolean alwaysUseFullPath) Whether URL lookups should always use the full path within the current web application context, i.e.voidsetDefaultEncoding(String defaultEncoding) Set the default character encoding to use for URL decoding.voidsetRemoveSemicolonContent(boolean removeSemicolonContent) Set if ";" (semicolon) content should be stripped from the request URI.voidsetUrlDecode(boolean urlDecode) Whether the context path and request URI should be decoded -- both of which are returned undecoded by the Servlet API, in contrast to the servlet path.booleanWhether configured to remove ";" (semicolon) content from the request URI.
- 
Field Details- 
PATH_ATTRIBUTE
- 
defaultInstanceShared, read-only instance with defaults. The following apply:- alwaysUseFullPath=false
- urlDecode=true
- removeSemicolon=true
- defaultEncoding=- WebUtils.DEFAULT_CHARACTER_ENCODING
 
- 
rawPathInstanceShared, read-only instance for the full, encoded path. The following apply:- alwaysUseFullPath=true
- urlDecode=false
- removeSemicolon=false
- defaultEncoding=- WebUtils.DEFAULT_CHARACTER_ENCODING
 
 
- 
- 
Constructor Details- 
UrlPathHelperpublic UrlPathHelper()
 
- 
- 
Method Details- 
setAlwaysUseFullPathpublic void setAlwaysUseFullPath(boolean alwaysUseFullPath) Whether URL lookups should always use the full path within the current web application context, i.e. withinServletContext.getContextPath().If set to false the path within the current servlet mapping is used instead if applicable (i.e. in the case of a prefix based Servlet mapping such as "/myServlet/*"). By default this is set to "false". 
- 
setUrlDecodepublic void setUrlDecode(boolean urlDecode) Whether the context path and request URI should be decoded -- both of which are returned undecoded by the Servlet API, in contrast to the servlet path.Either the request encoding or the default Servlet spec encoding (ISO-8859-1) is used when set to "true". By default this is set to true. Note: Be aware the servlet path will not match when compared to encoded paths. Therefore use of urlDecode=falseis not compatible with a prefix-based Servlet mapping and likewise implies also settingalwaysUseFullPath=true.- See Also:
 
- 
isUrlDecodepublic boolean isUrlDecode()Whether to decode the request URI when determining the lookup path.- Since:
- 4.3.13
 
- 
setRemoveSemicolonContentpublic void setRemoveSemicolonContent(boolean removeSemicolonContent) Set if ";" (semicolon) content should be stripped from the request URI.Default is "true". 
- 
shouldRemoveSemicolonContentpublic boolean shouldRemoveSemicolonContent()Whether configured to remove ";" (semicolon) content from the request URI.
- 
setDefaultEncodingSet the default character encoding to use for URL decoding. Default is ISO-8859-1, according to the Servlet spec.If the request specifies a character encoding itself, the request encoding will override this setting. This also allows for generically overriding the character encoding in a filter that invokes the ServletRequest.setCharacterEncodingmethod.- Parameters:
- defaultEncoding- the character encoding to use
- See Also:
 
- 
getDefaultEncodingReturn the default character encoding to use for URL decoding.
- 
resolveAndCacheLookupPathResolvethe lookupPath and cache it in a request attribute with the keyPATH_ATTRIBUTEfor subsequent access viagetResolvedLookupPath(ServletRequest).- Parameters:
- request- the current request
- Returns:
- the resolved path
- Since:
- 5.3
 
- 
getResolvedLookupPathReturn a previouslyresolvedlookupPath.- Parameters:
- request- the current request
- Returns:
- the previously resolved lookupPath
- Throws:
- IllegalArgumentException- if the lookup path is not found
- Since:
- 5.3
 
- 
getLookupPathForRequestReturn the mapping lookup path for the given request, within the current servlet mapping if applicable, else within the web application.Detects include request URL if called within a RequestDispatcher include. - Parameters:
- request- current HTTP request
- Returns:
- the lookup path
- See Also:
 
- 
getPathWithinServletMappingReturn the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.- Parameters:
- request- current HTTP request
- Returns:
- the path within the servlet mapping, or ""
- See Also:
 
- 
getPathWithinServletMappingReturn the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.Detects include request URL if called within a RequestDispatcher include. For example: servlet mapping = "/*"; request URI = "/test/a" → "/test/a". For example: servlet mapping = "/"; request URI = "/test/a" → "/test/a". For example: servlet mapping = "/test/*"; request URI = "/test/a" → "/a". For example: servlet mapping = "/test"; request URI = "/test" → "". For example: servlet mapping = "/*.test"; request URI = "/a.test" → "". - Parameters:
- request- current HTTP request
- pathWithinApp- a precomputed path within the application
- Returns:
- the path within the servlet mapping, or ""
- Since:
- 5.2.9
- See Also:
 
- 
getPathWithinApplicationReturn the path within the web application for the given request.Detects include request URL if called within a RequestDispatcher include. - Parameters:
- request- current HTTP request
- Returns:
- the path within the web application
- See Also:
 
- 
getRequestUriReturn the request URI for the given request, detecting an include request URL if called within a RequestDispatcher include.As the value returned by request.getRequestURI()is not decoded by the servlet container, this method will decode it.The URI that the web container resolves should be correct, but some containers like JBoss/Jetty incorrectly include ";" strings like ";jsessionid" in the URI. This method cuts off such incorrect appendices. - Parameters:
- request- current HTTP request
- Returns:
- the request URI
 
- 
getContextPathReturn the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.As the value returned by request.getContextPath()is not decoded by the servlet container, this method will decode it.- Parameters:
- request- current HTTP request
- Returns:
- the context path
 
- 
getServletPathReturn the servlet path for the given request, regarding an include request URL if called within a RequestDispatcher include.As the value returned by request.getServletPath()is already decoded by the servlet container, this method will not attempt to decode it.- Parameters:
- request- current HTTP request
- Returns:
- the servlet path
 
- 
getOriginatingRequestUriReturn the request URI for the given request. If this is a forwarded request, correctly resolves to the request URI of the original request.
- 
getOriginatingContextPathReturn the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.As the value returned by request.getContextPath()is not decoded by the servlet container, this method will decode it.- Parameters:
- request- current HTTP request
- Returns:
- the context path
 
- 
getOriginatingServletPathReturn the servlet path for the given request, detecting an include request URL if called within a RequestDispatcher include.- Parameters:
- request- current HTTP request
- Returns:
- the servlet path
 
- 
getOriginatingQueryStringReturn the query string part of the given request's URL. If this is a forwarded request, correctly resolves to the query string of the original request.- Parameters:
- request- current HTTP request
- Returns:
- the query string
 
- 
decodeRequestStringDecode the given source string with a URLDecoder. The encoding will be taken from the request, falling back to the default "ISO-8859-1".The default implementation uses URLDecoder.decode(input, enc).- Parameters:
- request- current HTTP request
- source- the String to decode
- Returns:
- the decoded String
- See Also:
 
- 
determineEncodingDetermine the encoding for the given request. Can be overridden in subclasses.The default implementation checks the request encoding, falling back to the default encoding specified for this resolver. - Parameters:
- request- current HTTP request
- Returns:
- the encoding for the request (never null)
- See Also:
 
- 
removeSemicolonContentRemove ";" (semicolon) content from the given request URI if the removeSemicolonContent property is set to "true". Note that "jsessionid" is always removed.- Parameters:
- requestUri- the request URI string to remove ";" content from
- Returns:
- the updated URI string
 
- 
decodePathVariablesDecode the given URI path variables viadecodeRequestString(jakarta.servlet.http.HttpServletRequest, java.lang.String)unlesssetUrlDecode(boolean)is set totruein which case it is assumed the URL path from which the variables were extracted is already decoded through a call togetLookupPathForRequest(HttpServletRequest).- Parameters:
- request- current HTTP request
- vars- the URI variables extracted from the URL path
- Returns:
- the same Map or a new Map instance
 
- 
decodeMatrixVariablespublic MultiValueMap<String,String> decodeMatrixVariables(HttpServletRequest request, MultiValueMap<String, String> vars) Decode the given matrix variables viadecodeRequestString(jakarta.servlet.http.HttpServletRequest, java.lang.String)unlesssetUrlDecode(boolean)is set totruein which case it is assumed the URL path from which the variables were extracted is already decoded through a call togetLookupPathForRequest(HttpServletRequest).- Parameters:
- request- current HTTP request
- vars- the URI variables extracted from the URL path
- Returns:
- the same Map or a new Map instance
 
 
-