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

时间:2022-08-29 10:46:13
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;
}