import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.net.URLDecoder;
/**
* Copyright (c) 2008
* Author: Wooce Yang
* Date: 2008-1-29
* Time: 10:12:47
* Description: 解决resin服务器上取不到urlencode后的中文参数的问题,需要自己从queryString中解析
*/
public class QueryStringParser {
Map paramMap;
HttpServletRequest request;
public QueryStringParser(HttpServletRequest request) {
String queryString = request.getQueryString();
if (queryString == null) {
paramMap = null;
this.request = request;
return;
} else
paramMap = new HashMap();
StringTokenizer st = new StringTokenizer(queryString, "&");
while (st.hasMoreTokens()) {
String pairs = st.nextToken();
String key = pairs.substring(0, pairs.indexOf('='));
String value = pairs.substring(pairs.indexOf('=') + 1);
paramMap.put(key, value);
}
}
public String get(String key) {
if(paramMap==null)
return request.getParameter(key);
else
return URLDecoder.decode((String) paramMap.get(key));
}
}
使用方法:
QueryStringParser parser = new QueryStringParser(request);
String curPath = parser.get("curpath");