Class Servlets

  • Direct Known Subclasses:
    Https

    public class Servlets
    extends java.lang.Object
    The servlet relevant utilities.
    Author:
    tomyeh
    See Also:
    Https, Charsets
    • Constructor Detail

      • Servlets

        protected Servlets()
        Utilities; no instantiation required.
    • Method Detail

      • isUniversalURL

        public static final boolean isUniversalURL​(java.lang.String uri)
        Returns whether a URL starts with xxx://, mailto:, about:, javascript:, data:, //
      • isServlet3

        public static final boolean isServlet3()
        Returns whether the current Web server supports Servlet 3.0 or above.
        Since:
        6.0.0
      • isServlet24

        public static final boolean isServlet24()
        Returns whether the current Web server supports Servlet 2.4 or above.
        Since:
        3.0.0
      • isServlet23

        public static final boolean isServlet23()
        Returns whether the current Web server supports Servlet 2.3 or above. Thus, if isServlet24() returns true, isServlet23() must return true, too.
        Since:
        3.0.0
      • locate

        public static final java.lang.String locate​(javax.servlet.ServletContext ctx,
                                                    javax.servlet.ServletRequest request,
                                                    java.lang.String pgpath,
                                                    org.zkoss.util.resource.Locator locator)
                                             throws javax.servlet.ServletException
        Locates a page based on the current Locale. It never returns null.

        Notice that it cannot resolve a path starting with '~', and containing '*', because it cannot access the content of the other servlet context.

        If an URI contains "*", it will be replaced with a proper Locale. For example, if the current Locale is zh_TW and the resource is named "ab*.cd", then it searches "ab_zh_TW.cd", "ab_zh.cd" and then "ab.cd", until any of them is found.

        Note: "*" must be right before ".", or the last character. For example, "ab*.cd" and "ab*" are both correct, while "ab*cd" and "ab*\/cd" are ignored.

        If an URI contains two "*", the first "*" will be replaced with a browser code and the second with a proper locale. The browser code depends on what browser the user are used to visit the web site. Currently, the code for Internet Explorer is "ie", Safari is "saf", Opera is "opr" and all others are "moz". Thus, in the above example, if the resource is named "ab**.cd" and Firefox is used, then it searches "abmoz_zh_TW.cd", "abmoz_zh.cd" and then "abmoz.cd", until any of them is found.

        Note: it assumes the path as name_lang_cn_var.ext where ".ext" is optional. Example, my_zh_tw.html.jsp.

        Parameters:
        ctx - the servlet context to locate pages
        pgpath - the page path excluding servlet name. It is OK to have the query string. It might contain "*" for current browser code and Locale.
        locator - the locator used to locate resource. If null, ctx is assumed.
        Returns:
        the path that matches the wildcard; pgpath, otherwise never null
        Throws:
        javax.servlet.ServletException
        See Also:
        Locales.getCurrent()
      • getBrowser

        public static java.lang.Double getBrowser​(javax.servlet.ServletRequest request,
                                                  java.lang.String name)
        Returns the version of the given browser name, or null if the client is not the given browsers.

        Notice that, after this method is called, an attribute named zk will be stored to the request, such that you can retrieve the browser information by use of EL, such as ${zk.ie > 7}.

        Parameters:
        request - the request.
        name - the browser's name. It includes "ie", "ff", "gecko", "webkit", "safari", "opera", "android", "mobile", "ios", "iphone", "ipad" and "ipod". And, "ff" is the same as "gecko", and "webkit" is the same as "safari".
        Since:
        6.0.0
      • getBrowser

        public static java.lang.String getBrowser​(javax.servlet.ServletRequest request)
        Returns the name of the browser, or null if not identifiable.
        Since:
        6.0.0
      • getBrowser

        public static java.lang.Double getBrowser​(java.lang.String userAgent,
                                                  java.lang.String name)
        Returns the version of the given browser name, or null if the client is not the given browsers.

        Notice that, after this method is called, an attribute named zk will be stored to the request, such that you can retrieve the browser information by use of EL, such as ${zk.ie > 7}.

        Parameters:
        userAgent - the user agent (i.e., the user-agent header in HTTP).
        name - the browser's name. It includes "ie", "ff", "gecko", "webkit", "safari", "opera", "android", "mobile", "ios", "iphone", "ipad" and "ipod". And, "ff" is the same as "gecko", and "webit" is the same as "safari".
        Since:
        6.0.0
      • getBrowser

        public static java.lang.String getBrowser​(java.lang.String userAgent)
        Returns the name of the browser, or null if not identifiable.
        Parameters:
        userAgent - the user agent (i.e., the user-agent header in HTTP).
        Since:
        6.0.0
      • isBrowser

        public static boolean isBrowser​(javax.servlet.ServletRequest req,
                                        java.lang.String type)
        Returns whether the client is a browser of the specified type. To more accurately specify the browser, please use getUserAgent(ServletRequest) instead.
        Parameters:
        type - the type of the browser. The syntax: <browser-name>[<version-number>][-].
        For example, ie9, ios and ie6-. And, ie9 means Internet Explorer 9 and later, while ie6- means Internet Explorer 6 (not prior, nor later).
        Since:
        3.5.1
      • isBrowser

        public static boolean isBrowser​(java.lang.String userAgent,
                                        java.lang.String type)
        Returns whether the user agent is a browser of the specified type. To more accurately specify the browser, please use getUserAgent(ServletRequest) instead.
        Parameters:
        userAgent - represents a client. For HTTP clients, It is the user-agent header.
        type - the type of the browser, or a list of types separated by comma.
        The syntax: <browser-name>[<version-number>][-].
        For example, ie9, ios and ie6-. And, ie9 means Internet Explorer 9 and later, while ie6- means Internet Explorer 6 (not prior, nor later).
        If a list of types are specified (such as ie6-,ie7-), this method returns true if any of them matches (i.e., OR condition).
        Since:
        3.5.1
      • isRobot

        public static final boolean isRobot​(javax.servlet.ServletRequest req)
        Deprecated.
        Returns whether the client is a robot (such as Web crawlers).

        Because there are too many robots, it returns true if the user-agent is not recognized.

      • isRobot

        public static final boolean isRobot​(java.lang.String userAgent)
        Deprecated.
        Returns whether the client is a robot (such as Web crawlers).

        Because there are too many robots, it returns true if the user-agent is not recognized.

        Parameters:
        userAgent - represents a client. For HTTP clients, It is the user-agent header.
        Since:
        3.5.1
      • isExplorer7

        public static final boolean isExplorer7​(java.lang.String userAgent)
        Deprecated.
        Returns whether the browser is Explorer 7 or later.
        Parameters:
        userAgent - represents a client. For HTTP clients, It is the user-agent header.
        Since:
        3.5.1
      • isGecko3

        public static final boolean isGecko3​(java.lang.String userAgent)
        Deprecated.
        Returns whether the browser is Gecko 3 based, such as Firefox 3.
        Parameters:
        userAgent - represents a client. For HTTP clients, It is the user-agent header.
        Since:
        3.5.1
      • isSafari

        public static final boolean isSafari​(java.lang.String userAgent)
        Deprecated.
        Returns whether the browser is Safari.
        Parameters:
        userAgent - represents a client. For HTTP clients, It is the user-agent header.
        Since:
        3.5.1
      • isOpera

        public static final boolean isOpera​(java.lang.String userAgent)
        Deprecated.
        Returns whether the browser is Opera.
        Parameters:
        userAgent - represents a client. For HTTP clients, It is the user-agent header.
        Since:
        3.5.1
      • isHilDevice

        public static final boolean isHilDevice​(javax.servlet.ServletRequest req)
        Deprecated.
        Returns whether the client is a mobile device supporting HIL (Handset Interactive Language). For example, ZK Mobile for Android.
        Since:
        3.0.2
      • isHilDevice

        public static final boolean isHilDevice​(java.lang.String userAgent)
        Deprecated.
        Returns whether the client is a mobile device supporting HIL (Handset Interactive Language). For example, ZK Mobile for Android.
        Parameters:
        userAgent - represents a client. For HTTP clients, It is the user-agent header.
        Since:
        3.5.1
      • getIECompatibilityInfo

        public static double[] getIECompatibilityInfo​(javax.servlet.ServletRequest request)
        Returns the IE compatibility information.
        Parameters:
        request -
        Returns:
        three double values in array [ie version, trident version, ie real version]
        Since:
        6.5.5
      • getUserAgent

        public static final java.lang.String getUserAgent​(javax.servlet.ServletRequest req)
        Returns the user-agent header, which indicates what the client is, or an empty string if not available.

        Note: it doesn't return null, so it is easy to test what the client is with String.indexOf(int).

        Since:
        3.0.2
      • isIncluded

        public static final boolean isIncluded​(javax.servlet.ServletRequest request)
        Tests whether this page is included by another page.
      • isForwarded

        public static final boolean isForwarded​(javax.servlet.ServletRequest request)
        Tests whether this page is forwarded by another page.
      • forward

        public static final void forward​(javax.servlet.ServletContext ctx,
                                         javax.servlet.ServletRequest request,
                                         javax.servlet.ServletResponse response,
                                         java.lang.String uri,
                                         java.util.Map params,
                                         int mode)
                                  throws java.io.IOException,
                                         javax.servlet.ServletException
        Forward to the specified URI. It enhances RequestDispatcher in the following ways.
        • It handles "~ctx"" where ctx is the context path of the foreign context. It is called foreign URI.
        • It detects whether the page calling this method is included by another servlet/page. If so, it uses RequestDispatcher.include() instead of RequestDispatcher.forward().
        • The forwarded page could accept additional parameters -- actually converting parameters to a query string and appending it to uri.
        • In additions, it does HTTP encoding, i.e., converts '+' and other characters to comply HTTP.

        NOTE: don't include query parameters in uri.

        Parameters:
        ctx - the servlet context. If null, uri cannot be foreign URI. It is ignored if URI is relevant (neither starts with '/' nor '~').
        uri - the URI to include. It is OK to relevant (without leading '/'). If starts with "/", the context path of request is assumed. To reference to foreign context, use "~ctx" where ctx is the context path of the foreign context (without leading '/'). If it could be any context path recognized by the Web container or any name registered with addExtendletContext(javax.servlet.ServletContext, java.lang.String, org.zkoss.web.util.resource.ExtendletContext).
        Notice that, since 3.6.3, uri could contain '*' (to denote locale and browser). Refer to locate(javax.servlet.ServletContext, javax.servlet.ServletRequest, java.lang.String, org.zkoss.util.resource.Locator).
        params - the parameter map; null to ignore
        mode - one of OVERWRITE_URI, IGNORE_PARAM, and APPEND_PARAM. It defines how to handle if both uri and params contains the same parameter.
        Throws:
        java.io.IOException
        javax.servlet.ServletException
      • forward

        public static final void forward​(javax.servlet.ServletContext ctx,
                                         javax.servlet.ServletRequest request,
                                         javax.servlet.ServletResponse response,
                                         java.lang.String uri)
                                  throws java.io.IOException,
                                         javax.servlet.ServletException
        A shortcut of forward(request, response, uri, null, 0).
        Throws:
        java.io.IOException
        javax.servlet.ServletException
      • include

        public static final void include​(javax.servlet.ServletContext ctx,
                                         javax.servlet.ServletRequest request,
                                         javax.servlet.ServletResponse response,
                                         java.lang.String uri,
                                         java.util.Map params,
                                         int mode)
                                  throws java.io.IOException,
                                         javax.servlet.ServletException
        Includes the resource at the specified URI. It enhances RequestDispatcher to allow the inclusion with a parameter map -- acutually converting parameters to a query string and appending it to uri.

        NOTE: don't include query parameters in uri.

        Parameters:
        ctx - the servlet context. If null, uri cannot be foreign URI. It is ignored if URI is relevant (neither starts with '/' nor '~').
        uri - the URI to include. It is OK to relevant (without leading '/'). If starts with "/", the context path of request is assumed. To reference to foreign context, use "~ctx/" where ctx is the context path of the foreign context (without leading '/').
        Notice that, since 3.6.3, uri could contain '*' (to denote locale and browser). Refer to locate(javax.servlet.ServletContext, javax.servlet.ServletRequest, java.lang.String, org.zkoss.util.resource.Locator).
        params - the parameter map; null to ignore
        mode - one of OVERWRITE_URI, IGNORE_PARAM, and APPEND_PARAM. It defines how to handle if both uri and params contains the same parameter.
        Throws:
        java.io.IOException
        javax.servlet.ServletException
      • include

        public static final void include​(javax.servlet.ServletContext ctx,
                                         javax.servlet.ServletRequest request,
                                         javax.servlet.ServletResponse response,
                                         java.lang.String uri)
                                  throws java.io.IOException,
                                         javax.servlet.ServletException
        A shortcut of include(request, response, uri, null, 0).
        Throws:
        java.io.IOException
        javax.servlet.ServletException
      • getRequestDispatcher

        public static final javax.servlet.RequestDispatcher getRequestDispatcher​(javax.servlet.ServletContext ctx,
                                                                                 javax.servlet.ServletRequest request,
                                                                                 java.lang.String uri,
                                                                                 java.util.Map params,
                                                                                 int mode)
                                                                          throws javax.servlet.ServletException
        Returns the request dispatch of the specified URI.
        Parameters:
        ctx - the servlet context. If null, uri cannot be foreign URI. It is ignored if uri is relevant (neither starts with '/' nor '~').
        request - the request. If null, uri cannot be relevant. It is used only if uri is relevant.
        uri - the URI to include. It is OK to relevant (without leading '/'). If starts with "/", the context path of request is assumed. To reference to foreign context, use "~ctx/" where ctx is the context path of the foreign context (without leading '/').
        params - the parameter map; null to ignore
        mode - one of OVERWRITE_URI, IGNORE_PARAM, and APPEND_PARAM. It defines how to handle if both uri and params contains the same parameter.
        Throws:
        javax.servlet.ServletException
      • getContextPaths

        public static final java.util.List<java.lang.String> getContextPaths()
        Returns a list of context paths (e.g., "/abc") that this application has. This implementation parse application.xml. For war that doesn't contain application.xml might have to override this method and parse another file to know what context being loaded.
      • getLimitTimeOffer

        public static final java.lang.String getLimitTimeOffer()
        Returns a token to represent a limit-time offer. It is mainly used as an parameter value (mostlycalled zk_lto), and then you could verify whether it is expired by isOfferExpired(java.lang.String, int).
      • isOfferExpired

        public static final boolean isOfferExpired​(java.lang.String lto,
                                                   int timeout)
        Returns whether a token returned by getLimitTimeOffer expired.
        Parameters:
        timeout - how long the office shall expire, unit: seconds.
      • addExtendletContext

        public static final ExtendletContext addExtendletContext​(javax.servlet.ServletContext ctx,
                                                                 java.lang.String name,
                                                                 ExtendletContext extctx)
        Adds an extended context.
        Returns:
        the previous extended context, if any, associated with the specified name.
      • removeExtendletContext

        public static final ExtendletContext removeExtendletContext​(javax.servlet.ServletContext ctx,
                                                                    java.lang.String name)
        Removes an extended context of the specified name.
      • getExtendletContext

        public static final ExtendletContext getExtendletContext​(javax.servlet.ServletContext ctx,
                                                                 java.lang.String name)
        Returns the extended context of the specified name.
      • getExtension

        public static final java.lang.String getExtension​(java.lang.String path)
        Returns the file/path extension of the specified path (excluding dot), or null if no extension at all.

        Note: the extension is converted to the lower case.

        Parameters:
        path - the path. If path is null, null is returned.
        Since:
        2.4.1
        See Also:
        getExtension(String, boolean)
      • getExtension

        public static final java.lang.String getExtension​(java.lang.String path,
                                                          boolean lastOnly)
        Returns the file/path extension of the specified path (excluding dot), or null if no extension at all.

        Note: the extension is converted to the lower case.

        The result is the same for both getExtension(String) and getExtension(String, boolean), if the path has only one dot. However, if there are more than one dot, e.g., /a/b.c.d, then getExtension(String) retrieves the last extension, that is, d in this example. On the other hand, if you invoke getExtension(path, false), it returns the complete extension, that is, c.d in this example.

        Parameters:
        path - the path. If path is null, null is returned.
        lastOnly - whether to return the last portion of extensioin if there are two or more dots. In other wors, getExtension(path) is the same as getExtension(path, true).
        Since:
        3.5.1
      • getDetail

        public static java.lang.String getDetail​(javax.servlet.ServletRequest request)
        Returns the request detail information. It is used to log the debug info.
        Since:
        3.0.5
      • getNormalPath

        public static java.lang.String getNormalPath​(java.lang.String path)
        Returns the normal path; that is, will elminate the double dots ".."(parent) and single dot "."(current) in the path as possible. e.g. /abc/../def would be normalized to /def; /abc/./def would be normalized to /abc/def; /abc//def would be normalized to /abc/def.

        Note that if found no way to navigate the path, it is deemed as an illegal path. e.g. /../abc or /../../abc is deemed as illegal path since we don't know how to continue doing the normalize.

        Since:
        3.6.2