I got the url value using request.getHeader("Referer")
e.g.:
我使用request.getHeader(“Referer”)获取了url值,例如:
string rr=request.getHeader("Referer");
<%= rr %>
I got the url as http://www.sun.com/questions?uid=21&value=gg
我把网址设为http://www.sun.com/questions?uid=21&value=gg
Now I stored that url in as string, how do I get the value parameter value as uid=21
and value=gg
现在我将该url存储为字符串,如何获取值参数值为uid = 21和value = gg
4 个解决方案
#1
You need to:
你需要:
- take the string after the '?'
- split that on '&' (you can do this and the above by using the URL object and calling getQuery())
- You'll then have strings of the form 'x=y'. Split on the first '='
- URLDecode the result parameter values.
在'?'后取字符串
在'&'上拆分(您可以通过使用URL对象并调用getQuery()来执行此操作和上述操作)
然后你会得到'x = y'形式的字符串。拆分第一个'='
URLDecode结果参数值。
This is all a bit messy, unfortunately.
不幸的是,这有点乱。
Why the URLDecode step ? Because the URL will be encoded such that '=' and '?' in parameter values won't confuse a parser.
为什么URLDecode步骤?因为URL将被编码为'='和'?'在参数值中不会混淆解析器。
#2
This tutorial might help a bit.
本教程可能会有所帮助。
What you need to do is parse the URL, then get the 'query' property, then parse it into name/value pairs.
您需要做的是解析URL,然后获取'query'属性,然后将其解析为名称/值对。
So something like this:
所以像这样:
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);
}
Disclaimer: this has not been tested, and you will need to do more error checking!
免责声明:尚未经过测试,您需要进行更多错误检查!
#3
Below an example how Apache Solr does it (SolrRequestParsers).
下面是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 );
}
Usage:
MultiMapSolrParams params = SolrRequestParsers.parseQueryString( req.getQueryString() );
And an example how Jersey does it (UriComponent):
以及Jersey如何做到这一点的例子(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);
}
}
#4
I combine two codes and result is below:
我结合了两个代码,结果如下:
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;
}
You get two arrays. One contains key(name), second contains values.
你得到两个数组。一个包含键(名称),第二个包含值。
#1
You need to:
你需要:
- take the string after the '?'
- split that on '&' (you can do this and the above by using the URL object and calling getQuery())
- You'll then have strings of the form 'x=y'. Split on the first '='
- URLDecode the result parameter values.
在'?'后取字符串
在'&'上拆分(您可以通过使用URL对象并调用getQuery()来执行此操作和上述操作)
然后你会得到'x = y'形式的字符串。拆分第一个'='
URLDecode结果参数值。
This is all a bit messy, unfortunately.
不幸的是,这有点乱。
Why the URLDecode step ? Because the URL will be encoded such that '=' and '?' in parameter values won't confuse a parser.
为什么URLDecode步骤?因为URL将被编码为'='和'?'在参数值中不会混淆解析器。
#2
This tutorial might help a bit.
本教程可能会有所帮助。
What you need to do is parse the URL, then get the 'query' property, then parse it into name/value pairs.
您需要做的是解析URL,然后获取'query'属性,然后将其解析为名称/值对。
So something like this:
所以像这样:
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);
}
Disclaimer: this has not been tested, and you will need to do more error checking!
免责声明:尚未经过测试,您需要进行更多错误检查!
#3
Below an example how Apache Solr does it (SolrRequestParsers).
下面是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 );
}
Usage:
MultiMapSolrParams params = SolrRequestParsers.parseQueryString( req.getQueryString() );
And an example how Jersey does it (UriComponent):
以及Jersey如何做到这一点的例子(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);
}
}
#4
I combine two codes and result is below:
我结合了两个代码,结果如下:
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;
}
You get two arrays. One contains key(name), second contains values.
你得到两个数组。一个包含键(名称),第二个包含值。