Class ServletContextPathHolder
- java.lang.Object
-
- com.helger.servlet.ServletContextPathHolder
-
@NotThreadSafe public final class ServletContextPathHolder extends Object
Helper class to hold the current servlet context path. In certain cases it is necessary to overwrite the context path (custom context path) if an application is run behind a reverse proxy but needs to emit absolute URLs.- Author:
- Philip Helger
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclearContextPath()Clears both servlet context and custom context path.static StringgetContextPath()Returns the context path of the web application.static StringgetContextPathOrNull()Returns the context path of the web application.static StringgetCustomContextPath()static StringgetServletContextPath()static booleanhasContextPath()static booleanhasCustomContextPath()static booleanhasServletContextPath()static booleanisSilentMode()static voidsetCustomContextPath(String sCustomContextPath)Manually change the context path to be used.static voidsetServletContextPath(String sServletContextPath)static booleansetSilentMode(boolean bSilentMode)
-
-
-
Method Detail
-
isSilentMode
public static boolean isSilentMode()
-
setSilentMode
public static boolean setSilentMode(boolean bSilentMode)
-
setServletContextPath
public static void setServletContextPath(@Nonnull String sServletContextPath)
-
hasServletContextPath
public static boolean hasServletContextPath()
- Returns:
trueif a servlet context path was set.- Since:
- 8.7.1
- See Also:
setServletContextPath(String)
-
getServletContextPath
@Nullable public static String getServletContextPath()
- Returns:
- The default servlet context path. May be
null. - Since:
- 8.7.1
- See Also:
setServletContextPath(String),getContextPath()
-
setCustomContextPath
public static void setCustomContextPath(@Nonnull String sCustomContextPath)
Manually change the context path to be used. Normally there is no need to call the method, because the context path is automatically determined from theServletContextor from the underlying request. This method is only needed, if a web application is proxied by e.g. an Apache httpd and the context path between httpd and Java web application server is different!- Parameters:
sCustomContextPath- The context path of the web application, or "" for the default (root) context. May not benull.
-
hasCustomContextPath
public static boolean hasCustomContextPath()
- Returns:
trueif a custom context path was set.- See Also:
setCustomContextPath(String)
-
getCustomContextPath
@Nullable public static String getCustomContextPath()
- Returns:
- The custom context path. May be
null. - Since:
- 8.7.1
- See Also:
setCustomContextPath(String),getContextPath()
-
hasContextPath
public static boolean hasContextPath()
- Returns:
trueif either custom context path or servlet context path are present.- Since:
- 9.1.2
-
getContextPathOrNull
@Nullable public static String getContextPathOrNull()
Returns the context path of the web application.The context path is the portion of the request URI that is used to select the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "".
It is possible that a servlet container may match a context by more than one context path. In such cases the context path will return the actual context path used by the request and it may differ from the path returned by this method. The context path returned by this method should be considered as the prime or preferred context path of the application.
- Returns:
- The context path of the web application, or "" for the default
(root) context or
nullif none of them is set. - Since:
- 9.1.0
- See Also:
getCustomContextPath(),getServletContextPath()
-
getContextPath
@Nonnull public static String getContextPath()
Returns the context path of the web application.The context path is the portion of the request URI that is used to select the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "".
It is possible that a servlet container may match a context by more than one context path. In such cases the context path will return the actual context path used by the request and it may differ from the path returned by this method. The context path returned by this method should be considered as the prime or preferred context path of the application.
- Returns:
- The context path of the web application, or "" for the default (root) context
- Throws:
IllegalStateException- if neither a custom context path nor a servlet context path is set- See Also:
getCustomContextPath(),getServletContextPath()
-
clearContextPath
public static void clearContextPath()
Clears both servlet context and custom context path.
-
-