我使用request.getHeader("Referer")
获取了网址值,例如:
string rr=request.getHeader("Referer");
<%= rr %>
我将网址设为http://www.sun.com/questions?uid=21&value=gg
现在我将该url存储为字符串,如何将value参数值设为uid=21
和value=gg
答案 0 :(得分:4)
你需要:
不幸的是,这有点乱。
为什么选择URLDecode?因为URL将被编码为'='和'?'在参数值中不会混淆解析器。
答案 1 :(得分:2)
This tutorial可能会有所帮助。
您需要做的是解析URL,然后获取'query'属性,然后将其解析为名称/值对。
这样的事情:
URL url = new URL(referer);
String queryStr = url.getQuery();
String[] params = queryStr.split("&");
for (String param: params) {
String key = param.substring(0, param.indexOf('=');
String val = param.substring(param.indexOf('=') + 1);
}
免责声明:尚未经过测试,您需要进行更多错误检查!
答案 2 :(得分:0)
下面是Apache Solr如何做到这一点的例子(SolrRequestParsers)。
/**
* Given a standard query string map it into solr params
*/
public static MultiMapSolrParams parseQueryString(String queryString)
{
Map<String,String[]> map = new HashMap<String, String[]>();
if( queryString != null && queryString.length() > 0 ) {
try {
for( String kv : queryString.split( "&" ) ) {
int idx = kv.indexOf( '=' );
if( idx > 0 ) {
String name = URLDecoder.decode( kv.substring( 0, idx ), "UTF-8");
String value = URLDecoder.decode( kv.substring( idx+1 ), "UTF-8");
MultiMapSolrParams.addParam( name, value, map );
}
else {
String name = URLDecoder.decode( kv, "UTF-8" );
MultiMapSolrParams.addParam( name, "", map );
}
}
}
catch( UnsupportedEncodingException uex ) {
throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, uex );
}
}
return new MultiMapSolrParams( map );
}
用法:
MultiMapSolrParams params = SolrRequestParsers.parseQueryString( req.getQueryString() );
泽西岛如何做到这一点的例子(UriComponent):
/**
* Decode the query component of a URI.
*
* @param q the query component in encoded form.
* @param decode true of the query parameters of the query component
* should be in decoded form.
* @return the multivalued map of query parameters.
*/
public static MultivaluedMap<String, String> decodeQuery(String q, boolean decode) {
MultivaluedMap<String, String> queryParameters = new MultivaluedMapImpl();
if (q == null || q.length() == 0) {
return queryParameters;
}
int s = 0, e = 0;
do {
e = q.indexOf('&', s);
if (e == -1) {
decodeQueryParam(queryParameters, q.substring(s), decode);
} else if (e > s) {
decodeQueryParam(queryParameters, q.substring(s, e), decode);
}
s = e + 1;
} while (s > 0 && s < q.length());
return queryParameters;
}
private static void decodeQueryParam(MultivaluedMap<String, String> params,
String param, boolean decode) {
try {
int equals = param.indexOf('=');
if (equals > 0) {
params.add(
URLDecoder.decode(param.substring(0, equals), "UTF-8"),
(decode) ? URLDecoder.decode(param.substring(equals + 1), "UTF-8") : param.substring(equals + 1));
} else if (equals == 0) {
// no key declared, ignore
} else if (param.length() > 0) {
params.add(
URLDecoder.decode(param, "UTF-8"),
"");
}
} catch (UnsupportedEncodingException ex) {
// This should never occur
throw new IllegalArgumentException(ex);
}
}
答案 3 :(得分:0)
我合并了两个代码,结果如下:
StringBuffer url = request.getRequestURL();
if(request.getQueryString()!= null){
url.append("?");
url.append(request.getQueryString());
}
String url_param = url.toString();
String[] params = url_param.split("&");
String[][] param_key = new String[1320][3];
String[][] param_val = new String[1320][3];
String param = "";
String key_tmp = "";
String val_tmp = "";
y = 1;
for(x=1;x<params.length;x++)
{
param = params[x];
key_tmp = param.substring(0, param.indexOf("="));
param_key[x][y] = key_tmp;
y++;
val_tmp = param.substring(param.indexOf("=") + 1);
param_val[x][y] = val_tmp;
y=1;
}
你得到两个数组。一个包含密钥(名称),第二个包含值。