Class 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 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:
        true if a servlet context path was set.
        Since:
        8.7.1
        See Also:
        setServletContextPath(String)
      • 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 the ServletContext or 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 be null.
      • hasCustomContextPath

        public static boolean hasCustomContextPath()
        Returns:
        true if a custom context path was set.
        See Also:
        setCustomContextPath(String)
      • hasContextPath

        public static boolean hasContextPath()
        Returns:
        true if 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 null if 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.