这里截获POST或GET请求提交的所有请求参数,并组成查询串返回

/***//**
*
*该方法用于将request中参数取出组成查询串后返回
*
*@paramrequest
*HttpServletRequest
*@returnString返回key1=value1&key2=value形式的查询串
*/

publicstaticStringgetQueryString(HttpServletRequestrequest)...{

try...{
booleanfirst=true;
StringBufferstrbuf=newStringBuffer("");
EnumerationemParams=request.getParameterNames();


do...{

if(!emParams.hasMoreElements())...{
break;
}
StringsParam=(String)emParams.nextElement();
String[]sValues=request.getParameterValues(sParam);
StringsValue="";

for(inti=0;i<sValues.length;i++)...{
sValue=sValues[i];
if(sValue!=null&&sValue.trim().length()!=0

&&first==true)...{
first=false;
strbuf.append(sParam).append("=").append(
URLEncoder.encode(sValue,GBK_ENCODE));
}
elseif(sValue!=null&&sValue.trim().length()!=0

&&first==false)...{
strbuf.append("&").append(sParam).append("=").append(
URLEncoder.encode(sValue,"GBK"));
}
}
}
while(true);

returnstrbuf.toString();

}catch(UnsupportedEncodingExceptione)...{
throwRuntimeException(e);
}
}

/***//**
*
*该方法用于将request中参数取出组成查询串后返回
*
*@paramrequest
*HttpServletRequest
*@returnString返回key1=value1&key2=value形式的查询串
*/
publicstaticStringgetQueryString(HttpServletRequestrequest)...{
try...{
booleanfirst=true;
StringBufferstrbuf=newStringBuffer("");
EnumerationemParams=request.getParameterNames();

do...{
if(!emParams.hasMoreElements())...{
break;
}
StringsParam=(String)emParams.nextElement();
String[]sValues=request.getParameterValues(sParam);
StringsValue="";
for(inti=0;i<sValues.length;i++)...{
sValue=sValues[i];
if(sValue!=null&&sValue.trim().length()!=0
&&first==true)...{
first=false;
strbuf.append(sParam).append("=").append(
URLEncoder.encode(sValue,GBK_ENCODE));
}
elseif(sValue!=null&&sValue.trim().length()!=0
&&first==false)...{
strbuf.append("&").append(sParam).append("=").append(
URLEncoder.encode(sValue,"GBK"));
}
}
}
while(true);
returnstrbuf.toString();
}catch(UnsupportedEncodingExceptione)...{
throwRuntimeException(e);
}
}
本文介绍了一种从HTTP请求中提取参数并将其格式化为查询字符串的方法。此方法适用于处理GET和POST请求,并能有效组织参数,便于进一步的数据处理。
205

被折叠的 条评论
为什么被折叠?



