Class Servlets

  • Direct Known Subclasses:
    Https

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

      Fields 
      Modifier and Type Field Description
      static int APPEND_PARAM
      Whether to append params if both uri and params contain the same parameter.
      static int IGNORE_PARAM
      Whether to ignore params if both uri and params contain the same parameter.
      static int OVERWRITE_URI
      Whether to overwrite uri if both uri and params contain the same parameter.
      static int PASS_THRU_ATTR
      Whether the specified parameters shall be passed thru the request attribute called arg.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Servlets()
      Utilities; no instantiation required.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static ExtendletContext addExtendletContext​(javax.servlet.ServletContext ctx, java.lang.String name, ExtendletContext extctx)
      Adds an extended context.
      static void forward​(javax.servlet.ServletContext ctx, javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, java.lang.String uri)
      A shortcut of forward(request, response, uri, null, 0).
      static void forward​(javax.servlet.ServletContext ctx, javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, java.lang.String uri, java.util.Map params, int mode)
      Forward to the specified URI.
      static java.lang.String generateURI​(java.lang.String uri, java.util.Map params, int mode)
      Generates URI by appending the parameters.
      static java.lang.String getBrowser​(java.lang.String userAgent)
      Returns the name of the browser, or null if not identifiable.
      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.
      static java.lang.String getBrowser​(javax.servlet.ServletRequest request)
      Returns the name of the browser, or null if not identifiable.
      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.
      static Servlets.ClientIdentifier getClientIdentifier()
      Returns the client identifier, or null if no such plugin.
      static java.util.List<java.lang.String> getContextPaths()
      Returns a list of context paths (e.g., "/abc") that this application has.
      static java.lang.String getDetail​(javax.servlet.ServletRequest request)
      Returns the request detail information.
      static ExtendletContext getExtendletContext​(javax.servlet.ServletContext ctx, java.lang.String name)
      Returns the extended context of the specified name.
      static 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.
      static 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.
      static java.lang.String getLimitTimeOffer()
      Returns a token to represent a limit-time offer.
      static java.lang.String getNormalPath​(java.lang.String path)
      Returns the normal path; that is, will elminate the double dots ".."
      static javax.servlet.RequestDispatcher getRequestDispatcher​(javax.servlet.ServletContext ctx, javax.servlet.ServletRequest request, java.lang.String uri, java.util.Map params, int mode)
      Returns the request dispatch of the specified URI.
      static java.net.URL getResource​(javax.servlet.ServletContext ctx, java.lang.String uri)
      Returns the resource of the specified uri.
      static java.io.InputStream getResourceAsStream​(javax.servlet.ServletContext ctx, java.lang.String uri)
      Returns the resource stream of the specified uri.
      static 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.
      static void include​(javax.servlet.ServletContext ctx, javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, java.lang.String uri)
      A shortcut of include(request, response, uri, null, 0).
      static void include​(javax.servlet.ServletContext ctx, javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, java.lang.String uri, java.util.Map params, int mode)
      Includes the resource at the specified URI.
      static boolean isBrowser​(java.lang.String userAgent, java.lang.String type)
      Returns whether the user agent is a browser of the specified type.
      static boolean isBrowser​(javax.servlet.ServletRequest req, java.lang.String type)
      Returns whether the client is a browser of the specified type.
      static boolean isForwarded​(javax.servlet.ServletRequest request)
      Tests whether this page is forwarded by another page.
      static boolean isIncluded​(javax.servlet.ServletRequest request)
      Tests whether this page is included by another page.
      static boolean isOfferExpired​(java.lang.String lto, int timeout)
      Returns whether a token returned by getLimitTimeOffer expired.
      static boolean isServlet23()
      Returns whether the current Web server supports Servlet 2.3 or above.
      static boolean isServlet24()
      Returns whether the current Web server supports Servlet 2.4 or above.
      static boolean isServlet3()
      Returns whether the current Web server supports Servlet 3.0 or above.
      static boolean isUniversalURL​(java.lang.String uri)
      Returns whether a URL starts with xxx://, mailto:, about:, javascript:, data:, //
      static java.lang.String locate​(javax.servlet.ServletContext ctx, javax.servlet.ServletRequest request, java.lang.String pgpath, Locator locator)
      Locates a page based on the current Locale.
      static ExtendletContext removeExtendletContext​(javax.servlet.ServletContext ctx, java.lang.String name)
      Removes an extended context of the specified name.
      static void setClientIdentifier​(Servlets.ClientIdentifier clientId)
      Sets the client identifier that is used to assist isBrowser(javax.servlet.ServletRequest, java.lang.String) to identify a client.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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,
                                                    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.ff > 5}.

        Parameters:
        request - the request.
        name - the browser's name. It includes "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.ff > 5}.

        Parameters:
        userAgent - the user agent (i.e., the user-agent header in HTTP).
        name - the browser's name. It includes "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
      • 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.

        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