截获POST或GET请求提交的所有参数

这里截获POST或GET请求提交的所有请求参数,并组成查询串返回
 /**
   *
   * 该方法用于将request中参数取出组成查询串后返回
   *
   * 
@param request
   *            HttpServletRequest
   * 
@return String 返回key1=value1&key2=value形式的查询串
   
*/

  
public static String getQueryString(HttpServletRequest request){
 
try{
    
boolean first = true;
    StringBuffer strbuf 
= new StringBuffer("");
    Enumeration emParams 
= request.getParameterNames();

    
do {
      
if (!emParams.hasMoreElements()) {
        
break;
      }

      String sParam 
= (String) emParams.nextElement();
      String[] sValues 
= request.getParameterValues(sParam);
      String sValue 
= "";
      
for (int i = 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));
        }

        
else if (sValue != null && sValue.trim().length() != 0
                 
&& first == false{
          strbuf.append(
"&").append(sParam).append("=").append(
              URLEncoder.encode(sValue, 
"GBK"));
        }

      }

    }

    
while (true);

    
return strbuf.toString();
}
catch(UnsupportedEncodingException e){
   
throw RuntimeException(e);
}

  }
 
posted @ 2007-04-02 12:31  lovingprince  阅读(1118)  评论(0编辑  收藏  举报