Class ServletUtil


  • public final class ServletUtil
    extends java.lang.Object
    Various servlet related helper methods.
    Version:
    $Id: ServletUtil.java#3 $
    Author:
    Harald Kuhr, Eirik Torske, last modified by $Author: haku $
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Map<java.lang.String,​java.lang.String> asMap​(javax.servlet.FilterConfig pConfig)
      Creates an unmodifiable Map view of the given FilterConfigs init-parameters.
      static java.util.Map<java.lang.String,​java.lang.String> asMap​(javax.servlet.ServletConfig pConfig)
      Creates an unmodifiable Map view of the given ServletConfigs init-parameters.
      static java.util.Map<java.lang.String,​java.lang.Object> attributesAsMap​(javax.servlet.ServletContext pContext)
      Creates an modifiable Map view of the given ServletContexts attributes.
      static java.util.Map<java.lang.String,​java.lang.Object> attributesAsMap​(javax.servlet.ServletRequest pRequest)
      Creates an modifiable Map view of the given ServletRequests attributes.
      static javax.servlet.ServletRequest createWrapper​(javax.servlet.ServletRequestWrapper pImplementation)
      Creates a wrapper that implements either ServletRequest or HttpServletRequest, depending on the type of pImplementation.getRequest().
      static javax.servlet.ServletResponse createWrapper​(javax.servlet.ServletResponseWrapper pImplementation)
      Creates a wrapper that implements either ServletResponse or HttpServletResponse, depending on the type of pImplementation.getResponse().
      static boolean getBooleanParameter​(javax.servlet.ServletRequest pReq, java.lang.String pName, boolean pDefault)
      Gets the value of the given parameter from the request converted to a boolean. If the parameter is not set or not parseable, the default value is returned.
      static java.lang.String getContextRelativeURI​(javax.servlet.http.HttpServletRequest pRequest)
      Gets the request URI relative to the current context path.
      static long getDateParameter​(javax.servlet.ServletRequest pReq, java.lang.String pName, long pDefault)
      Gets the value of the given parameter from the request converted to a Date. If the parameter is not set or not parseable, the default value is returned.
      static double getDoubleParameter​(javax.servlet.ServletRequest pReq, java.lang.String pName, double pDefault)
      Gets the value of the given parameter from the request converted to a double. If the parameter is not set or not parseable, the default value is returned.
      static float getFloatParameter​(javax.servlet.ServletRequest pReq, java.lang.String pName, float pDefault)
      Gets the value of the given parameter from the request converted to a float. If the parameter is not set or not parseable, the default value is returned.
      static java.lang.String getForwardContextPath​(javax.servlet.ServletRequest pRequest)
      Gets the context path of the resource this request was forwarded from.
      static java.lang.String getForwardPathInfo​(javax.servlet.ServletRequest pRequest)
      Gets the path info of the resource this request was forwarded from.
      static java.lang.String getForwardQueryString​(javax.servlet.ServletRequest pRequest)
      Gets the query string of the resource this request was forwarded from.
      static java.lang.String getForwardRequestURI​(javax.servlet.ServletRequest pRequest)
      Gets the URI of the resource this request was forwarded from.
      static java.lang.String getForwardServletPath​(javax.servlet.ServletRequest pRequest)
      Gets the servlet path of the resource this request was forwarded from.
      static java.lang.String getIncludeContextPath​(javax.servlet.ServletRequest pRequest)
      Gets the context path of the resource currently included.
      static java.lang.String getIncludePathInfo​(javax.servlet.ServletRequest pRequest)
      Gets the path info of the resource currently included.
      static java.lang.String getIncludeQueryString​(javax.servlet.ServletRequest pRequest)
      Gets the query string of the resource currently included.
      static java.lang.String getIncludeRequestURI​(javax.servlet.ServletRequest pRequest)
      Gets the URI of the resource currently included.
      static java.lang.String getIncludeServletPath​(javax.servlet.ServletRequest pRequest)
      Gets the servlet path of the resource currently included.
      static int getIntParameter​(javax.servlet.ServletRequest pReq, java.lang.String pName, int pDefault)
      Gets the value of the given parameter from the request converted to an int. If the parameter is not set or not parseable, the default value is returned.
      static long getLongParameter​(javax.servlet.ServletRequest pReq, java.lang.String pName, long pDefault)
      Gets the value of the given parameter from the request converted to an long. If the parameter is not set or not parseable, the default value is returned.
      static java.lang.String getParameter​(javax.servlet.ServletRequest pReq, java.lang.String pName, java.lang.String pDefault)
      Gets the value of the given parameter from the request, or if the parameter is not set, the default value.
      static java.net.URL getRealURL​(javax.servlet.ServletContext pContext, java.lang.String pPath)
      Returns a URL containing the real path for a given virtual path, on URL form.
      static java.lang.String getSessionId​(javax.servlet.http.HttpServletRequest pRequest)
      Gets the unique identifier assigned to this session.
      static java.io.File getTempDir​(javax.servlet.ServletContext pContext)
      Gets the temp directory for the given ServletContext (web app).
      static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> headersAsMap​(javax.servlet.http.HttpServletRequest pRequest)
      Creates an unmodifiable Map view of the given HttpServletRequests request headers.
      static java.util.Map<java.lang.String,​java.lang.String> initParamsAsMap​(javax.servlet.ServletContext pContext)
      Creates an unmodifiable Map view of the given ServletContexts init-parameters.
      static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> parametersAsMap​(javax.servlet.ServletRequest pRequest)
      Creates an unmodifiable Map view of the given HttpServletRequests request parameters.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getParameter

        public static java.lang.String getParameter​(javax.servlet.ServletRequest pReq,
                                                    java.lang.String pName,
                                                    java.lang.String pDefault)
        Gets the value of the given parameter from the request, or if the parameter is not set, the default value.
        Parameters:
        pReq - the servlet request
        pName - the parameter name
        pDefault - the default value
        Returns:
        the value of the parameter, or the default value, if the parameter is not set.
      • getBooleanParameter

        public static boolean getBooleanParameter​(javax.servlet.ServletRequest pReq,
                                                  java.lang.String pName,
                                                  boolean pDefault)
        Gets the value of the given parameter from the request converted to a boolean. If the parameter is not set or not parseable, the default value is returned.
        Parameters:
        pReq - the servlet request
        pName - the parameter name
        pDefault - the default value
        Returns:
        the value of the parameter converted to a boolean, or the default value, if the parameter is not set.
      • getIntParameter

        public static int getIntParameter​(javax.servlet.ServletRequest pReq,
                                          java.lang.String pName,
                                          int pDefault)
        Gets the value of the given parameter from the request converted to an int. If the parameter is not set or not parseable, the default value is returned.
        Parameters:
        pReq - the servlet request
        pName - the parameter name
        pDefault - the default value
        Returns:
        the value of the parameter converted to an int, or the default value, if the parameter is not set.
      • getLongParameter

        public static long getLongParameter​(javax.servlet.ServletRequest pReq,
                                            java.lang.String pName,
                                            long pDefault)
        Gets the value of the given parameter from the request converted to an long. If the parameter is not set or not parseable, the default value is returned.
        Parameters:
        pReq - the servlet request
        pName - the parameter name
        pDefault - the default value
        Returns:
        the value of the parameter converted to an long, or the default value, if the parameter is not set.
      • getFloatParameter

        public static float getFloatParameter​(javax.servlet.ServletRequest pReq,
                                              java.lang.String pName,
                                              float pDefault)
        Gets the value of the given parameter from the request converted to a float. If the parameter is not set or not parseable, the default value is returned.
        Parameters:
        pReq - the servlet request
        pName - the parameter name
        pDefault - the default value
        Returns:
        the value of the parameter converted to a float, or the default value, if the parameter is not set.
      • getDoubleParameter

        public static double getDoubleParameter​(javax.servlet.ServletRequest pReq,
                                                java.lang.String pName,
                                                double pDefault)
        Gets the value of the given parameter from the request converted to a double. If the parameter is not set or not parseable, the default value is returned.
        Parameters:
        pReq - the servlet request
        pName - the parameter name
        pDefault - the default value
        Returns:
        the value of the parameter converted to n double, or the default value, if the parameter is not set.
      • getDateParameter

        public static long getDateParameter​(javax.servlet.ServletRequest pReq,
                                            java.lang.String pName,
                                            long pDefault)
        Gets the value of the given parameter from the request converted to a Date. If the parameter is not set or not parseable, the default value is returned.
        Parameters:
        pReq - the servlet request
        pName - the parameter name
        pDefault - the default value
        Returns:
        the value of the parameter converted to a Date, or the default value, if the parameter is not set.
        See Also:
        StringUtil.toDate(String)
      • getIncludeRequestURI

        public static java.lang.String getIncludeRequestURI​(javax.servlet.ServletRequest pRequest)
        Gets the URI of the resource currently included. The value is read from the request attribute "javax.servlet.include.request_uri"
        Parameters:
        pRequest - the servlet request
        Returns:
        the URI of the included resource, or null if no include
        Since:
        Servlet 2.2
        See Also:
        HttpServletRequest.getRequestURI()
      • getIncludeContextPath

        public static java.lang.String getIncludeContextPath​(javax.servlet.ServletRequest pRequest)
        Gets the context path of the resource currently included. The value is read from the request attribute "javax.servlet.include.context_path"
        Parameters:
        pRequest - the servlet request
        Returns:
        the context path of the included resource, or null if no include
        Since:
        Servlet 2.2
        See Also:
        HttpServletRequest.getContextPath()
      • getIncludeServletPath

        public static java.lang.String getIncludeServletPath​(javax.servlet.ServletRequest pRequest)
        Gets the servlet path of the resource currently included. The value is read from the request attribute "javax.servlet.include.servlet_path"
        Parameters:
        pRequest - the servlet request
        Returns:
        the servlet path of the included resource, or null if no include
        Since:
        Servlet 2.2
        See Also:
        HttpServletRequest.getServletPath()
      • getIncludePathInfo

        public static java.lang.String getIncludePathInfo​(javax.servlet.ServletRequest pRequest)
        Gets the path info of the resource currently included. The value is read from the request attribute "javax.servlet.include.path_info"
        Parameters:
        pRequest - the servlet request
        Returns:
        the path info of the included resource, or null if no include
        Since:
        Servlet 2.2
        See Also:
        HttpServletRequest.getPathInfo()
      • getIncludeQueryString

        public static java.lang.String getIncludeQueryString​(javax.servlet.ServletRequest pRequest)
        Gets the query string of the resource currently included. The value is read from the request attribute "javax.servlet.include.query_string"
        Parameters:
        pRequest - the servlet request
        Returns:
        the query string of the included resource, or null if no include
        Since:
        Servlet 2.2
        See Also:
        HttpServletRequest.getQueryString()
      • getForwardRequestURI

        public static java.lang.String getForwardRequestURI​(javax.servlet.ServletRequest pRequest)
        Gets the URI of the resource this request was forwarded from. The value is read from the request attribute "javax.servlet.forward.request_uri"
        Parameters:
        pRequest - the servlet request
        Returns:
        the URI of the resource, or null if not forwarded
        Since:
        Servlet 2.4
        See Also:
        HttpServletRequest.getRequestURI()
      • getForwardContextPath

        public static java.lang.String getForwardContextPath​(javax.servlet.ServletRequest pRequest)
        Gets the context path of the resource this request was forwarded from. The value is read from the request attribute "javax.servlet.forward.context_path"
        Parameters:
        pRequest - the servlet request
        Returns:
        the context path of the resource, or null if not forwarded
        Since:
        Servlet 2.4
        See Also:
        HttpServletRequest.getContextPath()
      • getForwardServletPath

        public static java.lang.String getForwardServletPath​(javax.servlet.ServletRequest pRequest)
        Gets the servlet path of the resource this request was forwarded from. The value is read from the request attribute "javax.servlet.forward.servlet_path"
        Parameters:
        pRequest - the servlet request
        Returns:
        the servlet path of the resource, or null if not forwarded
        Since:
        Servlet 2.4
        See Also:
        HttpServletRequest.getServletPath()
      • getForwardPathInfo

        public static java.lang.String getForwardPathInfo​(javax.servlet.ServletRequest pRequest)
        Gets the path info of the resource this request was forwarded from. The value is read from the request attribute "javax.servlet.forward.path_info"
        Parameters:
        pRequest - the servlet request
        Returns:
        the path info of the resource, or null if not forwarded
        Since:
        Servlet 2.4
        See Also:
        HttpServletRequest.getPathInfo()
      • getForwardQueryString

        public static java.lang.String getForwardQueryString​(javax.servlet.ServletRequest pRequest)
        Gets the query string of the resource this request was forwarded from. The value is read from the request attribute "javax.servlet.forward.query_string"
        Parameters:
        pRequest - the servlet request
        Returns:
        the query string of the resource, or null if not forwarded
        Since:
        Servlet 2.4
        See Also:
        HttpServletRequest.getQueryString()
      • getContextRelativeURI

        public static java.lang.String getContextRelativeURI​(javax.servlet.http.HttpServletRequest pRequest)
        Gets the request URI relative to the current context path.

        As an example:

         requestURI = "/webapp/index.jsp"
         contextPath = "/webapp"
         
        The method will return "/index.jsp".
        Parameters:
        pRequest - the current HTTP request
        Returns:
        the request URI relative to the current context path.
      • getRealURL

        public static java.net.URL getRealURL​(javax.servlet.ServletContext pContext,
                                              java.lang.String pPath)
                                       throws java.net.MalformedURLException
        Returns a URL containing the real path for a given virtual path, on URL form. Note that this method will return null for all the same reasons as ServletContext.getRealPath(java.lang.String) does.
        Parameters:
        pContext - the servlet context
        pPath - the virtual path
        Returns:
        a URL object containing the path, or null.
        Throws:
        java.net.MalformedURLException - if the path refers to a malformed URL
        See Also:
        ServletContext.getRealPath(java.lang.String), ServletContext.getResource(java.lang.String)
      • getTempDir

        public static java.io.File getTempDir​(javax.servlet.ServletContext pContext)
        Gets the temp directory for the given ServletContext (web app).
        Parameters:
        pContext - the servlet context
        Returns:
        the temp directory
      • getSessionId

        public static java.lang.String getSessionId​(javax.servlet.http.HttpServletRequest pRequest)
        Gets the unique identifier assigned to this session. The identifier is assigned by the servlet container and is implementation dependent.
        Parameters:
        pRequest - The HTTP servlet request object.
        Returns:
        the session Id
      • asMap

        public static java.util.Map<java.lang.String,​java.lang.String> asMap​(javax.servlet.ServletConfig pConfig)
        Creates an unmodifiable Map view of the given ServletConfigs init-parameters. Note: The returned Map is optimized for get operations and iterating over it's keySet. For other operations it may not perform well.
        Parameters:
        pConfig - the servlet configuration
        Returns:
        a Map view of the config
        Throws:
        java.lang.IllegalArgumentException - if pConfig is null
      • asMap

        public static java.util.Map<java.lang.String,​java.lang.String> asMap​(javax.servlet.FilterConfig pConfig)
        Creates an unmodifiable Map view of the given FilterConfigs init-parameters. Note: The returned Map is optimized for get operations and iterating over it's keySet. For other operations it may not perform well.
        Parameters:
        pConfig - the servlet filter configuration
        Returns:
        a Map view of the config
        Throws:
        java.lang.IllegalArgumentException - if pConfig is null
      • initParamsAsMap

        public static java.util.Map<java.lang.String,​java.lang.String> initParamsAsMap​(javax.servlet.ServletContext pContext)
        Creates an unmodifiable Map view of the given ServletContexts init-parameters. Note: The returned Map is optimized for get operations and iterating over it's keySet. For other operations it may not perform well.
        Parameters:
        pContext - the servlet context
        Returns:
        a Map view of the init parameters
        Throws:
        java.lang.IllegalArgumentException - if pContext is null
      • attributesAsMap

        public static java.util.Map<java.lang.String,​java.lang.Object> attributesAsMap​(javax.servlet.ServletContext pContext)
        Creates an modifiable Map view of the given ServletContexts attributes.
        Parameters:
        pContext - the servlet context
        Returns:
        a Map view of the attributes
        Throws:
        java.lang.IllegalArgumentException - if pContext is null
      • attributesAsMap

        public static java.util.Map<java.lang.String,​java.lang.Object> attributesAsMap​(javax.servlet.ServletRequest pRequest)
        Creates an modifiable Map view of the given ServletRequests attributes.
        Parameters:
        pRequest - the servlet request
        Returns:
        a Map view of the attributes
        Throws:
        java.lang.IllegalArgumentException - if pContext is null
      • parametersAsMap

        public static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> parametersAsMap​(javax.servlet.ServletRequest pRequest)
        Creates an unmodifiable Map view of the given HttpServletRequests request parameters.
        Parameters:
        pRequest - the request
        Returns:
        a Map view of the request parameters
        Throws:
        java.lang.IllegalArgumentException - if pRequest is null
      • headersAsMap

        public static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> headersAsMap​(javax.servlet.http.HttpServletRequest pRequest)
        Creates an unmodifiable Map view of the given HttpServletRequests request headers.
        Parameters:
        pRequest - the request
        Returns:
        a Map view of the request headers
        Throws:
        java.lang.IllegalArgumentException - if pRequest is null
      • createWrapper

        public static javax.servlet.ServletResponse createWrapper​(javax.servlet.ServletResponseWrapper pImplementation)
        Creates a wrapper that implements either ServletResponse or HttpServletResponse, depending on the type of pImplementation.getResponse().
        Parameters:
        pImplementation - the servlet response to create a wrapper for
        Returns:
        a ServletResponse or HttpServletResponse, depending on the type of pImplementation.getResponse()
      • createWrapper

        public static javax.servlet.ServletRequest createWrapper​(javax.servlet.ServletRequestWrapper pImplementation)
        Creates a wrapper that implements either ServletRequest or HttpServletRequest, depending on the type of pImplementation.getRequest().
        Parameters:
        pImplementation - the servlet request to create a wrapper for
        Returns:
        a ServletResponse or HttpServletResponse, depending on the type of pImplementation.getResponse()