java代码收藏:获取HttpServletRequest中某一前缀的参数

public static Map getParametersStartingWith(ServletRequest request, String prefix)

    {

        Enumeration paramNames = request.getParameterNames();

        Map params = new TreeMap();

        if(prefix == null)prefix = "";

        while(paramNames != null && paramNames.hasMoreElements()) 

        {

            String paramName = (String)paramNames.nextElement();

            if("".equals(prefix) || paramName.startsWith(prefix))

            {

                String unprefixed = paramName.substring(prefix.length());

                String values[] = request.getParameterValues(paramName);

                if(values != null && values.length != 0)

                    if(values.length > 1)

                        params.put(unprefixed, values);

                    else

                        params.put(unprefixed, values[0]);

            }

        }

        return params;

    }


 

你可能感兴趣的:(java代码收藏:获取HttpServletRequest中某一前缀的参数)