Class Servlets

    • Method Detail

      • getRequestHostname

        public static String getRequestHostname​(HttpServletRequest request)
        Returns the HTTP request hostname. This is the entire domain, without any scheme and slashes. Noted should be that this value is extracted from the request URL, not from ServletRequest.getServerName() as its outcome can be influenced by proxies.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        The HTTP request hostname.
        Throws:
        IllegalArgumentException - When the URL is malformed. This is however unexpected as the request would otherwise not have hit the server at all.
        See Also:
        HttpServletRequest.getRequestURL()
      • getRequestDomainURL

        public static String getRequestDomainURL​(HttpServletRequest request)
        Returns the HTTP request domain URL. This is the URL with the scheme and domain, without any trailing slash.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        The HTTP request domain URL.
        Throws:
        IllegalArgumentException - When the URL is malformed. This is however unexpected as the request would otherwise not have hit the server at all.
        See Also:
        HttpServletRequest.getRequestURL()
      • getRequestPathInfo

        public static String getRequestPathInfo​(HttpServletRequest request)
        Returns the HTTP request path info, taking into account whether FacesViews is used with MultiViews enabled. If the resource is prefix mapped (e.g. /faces/*), then this returns the whole part after the prefix mapping, with a leading slash. If the resource is suffix mapped (e.g. *.xhtml), then this returns null.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        The HTTP request path info.
        Since:
        2.5
        See Also:
        HttpServletRequest.getPathInfo(), FacesViews.FACES_VIEWS_ORIGINAL_PATH_INFO
      • getRequestQueryStringMap

        public static Map<String,​List<String>> getRequestQueryStringMap​(HttpServletRequest request)
        Returns the HTTP request query string as parameter values map. Note this method returns only the request URL (GET) parameters, as opposed to ServletRequest.getParameterMap(), which contains both the request URL (GET) parameters and and the request body (POST) parameters. The map entries are in the same order as they appear in the query string.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        The HTTP request query string as parameter values map.
      • getRequestParameterMap

        public static Map<String,​List<String>> getRequestParameterMap​(HttpServletRequest request)
        Returns the HTTP request parameter map. Note this method returns the values as a List<String>, as opposed to ServletRequest.getParameterMap(), which returns the values as String[]. The map entries are not per definition ordered, but the values are.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        The HTTP request parameter map.
        Since:
        2.6
      • getRequestURIWithQueryString

        public static String getRequestURIWithQueryString​(HttpServletRequest request)
        Returns the HTTP request URI with query string, regardless of any forward. This is the part after the domain in the request URL, including the leading slash and the request query string.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        The HTTP request URI with query string.
        See Also:
        getRequestURI(HttpServletRequest), getRequestQueryString(HttpServletRequest)
      • getRequestRelativeURI

        public static String getRequestRelativeURI​(HttpServletRequest request)
        Returns the HTTP request URI relative to the context root, regardless of any forward. This is the request URI minus the context path. Note that this includes path parameters.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        The HTTP request URI relative to the context root.
        Since:
        1.8
      • getRequestRelativeURIWithoutPathParameters

        public static String getRequestRelativeURIWithoutPathParameters​(HttpServletRequest request)
        Returns the HTTP request URI relative to the context root without path parameters, regardless of any forward. This is the request URI minus the context path and path parameters.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        The HTTP request URI relative to the context root without path parameters.
        Since:
        1.8
      • getRequestURL

        public static String getRequestURL​(HttpServletRequest request)
        Returns the HTTP request URL with query string, regardless of any forward. This is the full request URL without query string as the enduser sees in browser address bar.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        The HTTP request URL without query string, regardless of any forward.
        Since:
        2.4
        See Also:
        HttpServletRequest.getRequestURL()
      • getRequestURLWithQueryString

        public static String getRequestURLWithQueryString​(HttpServletRequest request)
        Returns the HTTP request URL with query string. This is the full request URL with query string as the enduser sees in browser address bar.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        The HTTP request URL with query string, regardless of any forward.
        See Also:
        HttpServletRequest.getRequestURL(), HttpServletRequest.getQueryString()
      • toParameterMap

        public static Map<String,​List<String>> toParameterMap​(String queryString)
        Converts the given request query string to request parameter values map.
        Parameters:
        queryString - The request query string.
        Returns:
        The request query string as request parameter values map.
        Since:
        1.7
      • toQueryString

        public static String toQueryString​(Map<String,​List<String>> parameterMap)
        Converts the given request parameter values map to request query string. Empty names and null values will be skipped.
        Parameters:
        parameterMap - The request parameter values map.
        Returns:
        The request parameter values map as request query string.
        Since:
        2.0
      • toQueryString

        public static String toQueryString​(List<? extends ParamHolder<?>> params)
        Converts the given parameter values list to request query string. Empty names and null values will be skipped.
        Parameters:
        params - The parameter values list.
        Returns:
        The parameter values list as request query string.
        Since:
        2.2
      • getRemoteAddr

        public static String getRemoteAddr​(HttpServletRequest request)
        Returns the Internet Protocol (IP) address of the client that sent the request. This will first check the Forwarded and X-Forwarded-For request headers and if any is present, then return its first IP address, else just return ServletRequest.getRemoteAddr() unmodified.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        The IP address of the client.
        Since:
        2.3
        See Also:
        ServletRequest.getRemoteAddr()
      • isProxied

        public static boolean isProxied​(HttpServletRequest request)
        Returns true if request is proxied, false otherwise. In other words, returns true when either Forwarded or X-Forwarded-For request headers is present.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        true if request is proxied, false otherwise.
        Since:
        3.6
        See Also:
        HttpServletRequest.getHeader(String)
      • isSecure

        public static boolean isSecure​(HttpServletRequest request)
        Returns true if connection is secure, false otherwise. This method will first check if ServletRequest.isSecure() returns true, and if not true, check if the X-Forwarded-Proto is present and equals to https.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        true if connection is secure, false otherwise.
        Since:
        3.0
        See Also:
        ServletRequest.isSecure()
      • getSubmittedFileName

        public static String getSubmittedFileName​(Part part)
        Returns the submitted file name of the given part, making sure that any path is stripped off. Some browsers are known to incorrectly include the client side path along with it. Since version 2.6.7, RFC-2231/5987 encoded file names are also supported.
        Parameters:
        part - The part of a multipart/form-data request.
        Returns:
        The submitted file name of the given part, or null if there is none.
        Since:
        2.5
      • headerToMap

        public static Map<String,​String> headerToMap​(String header)
        Returns a mapping of given semicolon-separated request header. The returned map is unordered and unmodifiable.
        Parameters:
        header - Any semicolon-separated request header, e.g. Content-Disposition.
        Returns:
        A mapping of given semicolon-separated request header.
        Since:
        3.0
      • setCacheHeaders

        public static void setCacheHeaders​(HttpServletResponse response,
                                           long expires)

        Set the cache headers. If the expires argument is larger than 0 seconds, then the following headers will be set:

        • Cache-Control: public,max-age=[expiration time in seconds],must-revalidate
        • Expires: [expiration date of now plus expiration time in seconds]

        Else the method will delegate to setNoCacheHeaders(HttpServletResponse).

        Parameters:
        response - The HTTP servlet response to set the headers on.
        expires - The expire time in seconds (not milliseconds!).
        Since:
        2.2
      • setNoCacheHeaders

        public static void setNoCacheHeaders​(HttpServletResponse response)

        Set the no-cache headers. The following headers will be set:

        • Cache-Control: no-cache,no-store,must-revalidate
        • Expires: [expiration date of 0]
        • Pragma: no-cache
        Set the no-cache headers.
        Parameters:
        response - The HTTP servlet response to set the headers on.
        Since:
        2.2
      • formatContentDispositionHeader

        public static String formatContentDispositionHeader​(String filename,
                                                            boolean attachment)

        Format an UTF-8 compatible content disposition header for the given filename and whether it's an attachment.

        Parameters:
        filename - The filename to appear in "Save As" dialogue.
        attachment - Whether the content should be provided as an attachment or inline.
        Returns:
        An UTF-8 compatible content disposition header.
        Since:
        2.6
      • getRequestCookie

        public static String getRequestCookie​(HttpServletRequest request,
                                              String name)
        Returns the value of the HTTP request cookie associated with the given name. The value is implicitly URL-decoded with a charset of UTF-8.
        Parameters:
        request - The involved HTTP servlet request.
        name - The HTTP request cookie name.
        Returns:
        The value of the HTTP request cookie associated with the given name.
        Throws:
        UnsupportedOperationException - When this platform does not support UTF-8.
        Since:
        2.0
        See Also:
        HttpServletRequest.getCookies()
      • addResponseCookie

        public static void addResponseCookie​(HttpServletRequest request,
                                             HttpServletResponse response,
                                             String name,
                                             String value,
                                             int maxAge)
        Add a cookie with given name, value and maxage to the HTTP response. The cookie value will implicitly be URL-encoded with UTF-8 so that any special characters can be stored. The cookie will implicitly be set in the domain and path of the current request URL. The cookie will implicitly be set to HttpOnly as JavaScript is not supposed to manipulate server-created cookies. The cookie will implicitly be set to secure when the current request is a HTTPS request.
        Parameters:
        request - The involved HTTP servlet request.
        response - The involved HTTP servlet response.
        name - The cookie name.
        value - The cookie value.
        maxAge - The maximum age of the cookie, in seconds. If this is 0, then the cookie will be removed. Note that the name and path must be exactly the same as it was when the cookie was created. If this is -1 then the cookie will become a session cookie and thus live as long as the established HTTP session.
        Throws:
        UnsupportedOperationException - When this platform does not support UTF-8.
        Since:
        2.0
        See Also:
        HttpServletResponse.addCookie(Cookie)
      • addResponseCookie

        public static void addResponseCookie​(HttpServletRequest request,
                                             HttpServletResponse response,
                                             String name,
                                             String value,
                                             String path,
                                             int maxAge)
        Add a cookie with given name, value, path and maxage to the HTTP response. The cookie value will implicitly be URL-encoded with UTF-8 so that any special characters can be stored. The cookie will implicitly be set in the domain of the current request URL. The cookie will implicitly be set to HttpOnly as JavaScript is not supposed to manipulate server-created cookies. The cookie will implicitly be set to secure when the current request is a HTTPS request.
        Parameters:
        request - The involved HTTP servlet request.
        response - The involved HTTP servlet response.
        name - The cookie name.
        value - The cookie value.
        path - The cookie path. If this is /, then the cookie is available in all pages of the webapp. If this is /somespecificpath, then the cookie is only available in pages under the specified path.
        maxAge - The maximum age of the cookie, in seconds. If this is 0, then the cookie will be removed. Note that the name and path must be exactly the same as it was when the cookie was created. If this is -1 then the cookie will become a session cookie and thus live as long as the established HTTP session.
        Throws:
        UnsupportedOperationException - When this platform does not support UTF-8.
        Since:
        2.0
        See Also:
        HttpServletResponse.addCookie(Cookie)
      • addResponseCookie

        public static void addResponseCookie​(HttpServletRequest request,
                                             HttpServletResponse response,
                                             String name,
                                             String value,
                                             String domain,
                                             String path,
                                             int maxAge)
        Add a cookie with given name, value, domain, path and maxage to the HTTP response. The cookie value will implicitly be URL-encoded with UTF-8 so that any special characters can be stored. The cookie will implicitly be set to HttpOnly as JavaScript is not supposed to manipulate server-created cookies. The cookie will implicitly be set to secure when the current request is a HTTPS request.
        Parameters:
        request - The involved HTTP servlet request.
        response - The involved HTTP servlet response.
        name - The cookie name.
        value - The cookie value.
        domain - The cookie domain. You can use .example.com (with a leading period) if you'd like the cookie to be available to all subdomains of the domain. Note that you cannot set it to a different domain.
        path - The cookie path. If this is /, then the cookie is available in all pages of the webapp. If this is /somespecificpath, then the cookie is only available in pages under the specified path.
        maxAge - The maximum age of the cookie, in seconds. If this is 0, then the cookie will be removed. Note that the name and path must be exactly the same as it was when the cookie was created. If this is -1 then the cookie will become a session cookie and thus live as long as the established HTTP session.
        Throws:
        UnsupportedOperationException - When this platform does not support UTF-8.
        Since:
        2.0
        See Also:
        HttpServletResponse.addCookie(Cookie)
      • removeResponseCookie

        public static void removeResponseCookie​(HttpServletRequest request,
                                                HttpServletResponse response,
                                                String name,
                                                String path)
        Remove the cookie with given name and path from the HTTP response. Note that the name and path must be exactly the same as it was when the cookie was created.
        Parameters:
        request - The involved HTTP servlet request.
        response - The involved HTTP servlet response.
        name - The cookie name.
        path - The cookie path.
        Since:
        2.0
        See Also:
        HttpServletResponse.addCookie(Cookie)
      • getApplicationAttribute

        public static <T> T getApplicationAttribute​(ServletContext context,
                                                    String name)
        Returns the application scope attribute value associated with the given name.
        Type Parameters:
        T - The expected return type.
        Parameters:
        context - The servlet context used for looking up the attribute.
        name - The application scope attribute name.
        Returns:
        The application scope attribute value associated with the given name.
        Throws:
        ClassCastException - When T is of wrong type.
        See Also:
        ServletContext.getAttribute(String)
      • isFacesAjaxRequest

        public static boolean isFacesAjaxRequest​(HttpServletRequest request)
        Returns true if the given HTTP servlet request is a Faces ajax request. This does exactly the same as Faces.isAjaxRequest(), but then without the need for a FacesContext. The major advantage is that you can perform the job inside a servlet filter, where the FacesContext is normally not available.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        true if the given HTTP servlet request is a Faces ajax request.
        Since:
        2.0
      • isFacesResourceRequest

        public static boolean isFacesResourceRequest​(HttpServletRequest request)
        Returns true if the given HTTP servlet request is a Faces resource request. I.e. this request will trigger the Faces ResourceHandler for among others CSS/JS/image resources.
        Parameters:
        request - The involved HTTP servlet request.
        Returns:
        true if the given HTTP servlet request is a Faces resource request.
        Since:
        2.0
        See Also:
        ResourceHandler.RESOURCE_IDENTIFIER
      • isFacesDevelopment

        public static boolean isFacesDevelopment​(ServletContext context)
        Returns true if we're in Faces development stage. This will be the case when the jakarta.faces.PROJECT_STAGE context parameter in web.xml is set to Development.
        Parameters:
        context - The involved servlet context.
        Returns:
        true if we're in development stage, otherwise false.
        Since:
        2.1
        See Also:
        Application.getProjectStage()
      • facesRedirect

        public static void facesRedirect​(HttpServletRequest request,
                                         HttpServletResponse response,
                                         String url,
                                         Object... paramValues)
        Sends a temporary (302) Faces redirect to the given URL, supporting Faces ajax requests. This does exactly the same as Faces.redirect(String, Object...), but without the need for a FacesContext. The major advantage is that you can perform the job inside a servlet filter or even a plain vanilla servlet, where the FacesContext is normally not available. This method also recognizes Faces ajax requests which requires a special XML response in order to successfully perform the redirect.

        If the given URL does not start with http://, https:// or /, then the request context path will be prepended, otherwise it will be the unmodified redirect URL. So, when redirecting to another page in the same web application, always specify the full path from the context root on (which in turn does not need to start with /).

         Servlets.facesRedirect(request, response, "some.xhtml");
         

        You can use String.format(String, Object...) placeholder %s in the redirect URL to represent placeholders for any request parameter values which needs to be URL-encoded. Here's a concrete example:

         Servlets.facesRedirect(request, response, "some.xhtml?foo=%s&bar=%s", foo, bar);
         
        Parameters:
        request - The involved HTTP servlet request.
        response - The involved HTTP servlet response.
        url - The URL to redirect the current response to.
        paramValues - The request parameter values which you'd like to put URL-encoded in the given URL.
        Throws:
        UncheckedIOException - Whenever something fails at I/O level.
        Since:
        2.0
      • getWebXmlURL

        public static URL getWebXmlURL​(ServletContext context)
                                throws IOException
        Returns URL of web.xml file used in current application. This also takes into account the Quarkus location in META-INF folder.
        Parameters:
        context - The involved servlet context.
        Returns:
        URL of web.xml file used in current application.
        Throws:
        IOException
        Since:
        3.14