Parsing a requestUrl in Java
I have a request URL i need to parse that URL and have to modify that
EG :if I have request URL like http://www.xyz.c开发者_C百科om/?a=b&c=d
Now I need to modify the value of c or a to something else before redirecting to this URL How can I achieve this.
Thanks, Ashish
Here is what you can do:
public static Map<String, String> getQueryMap(String query)
{
String[] params = query.split("&");
Map<String, String> map = new HashMap<String, String>();
for (String param : params)
{
String name = param.split("=")[0];
String value = param.split("=")[1];
map.put(name, value);
}
return map;
}
String query = url.getQuery();
Map<String, String> map = getQueryMap(query);
Set<String> keys = map.keySet();
for (String key : keys)
{
System.out.println("Name=" + key);
System.out.println("Value=" + map.get(key));
}
invoke request.getParameterMap and iterate over the querystring keys and replace with what ever needed
精彩评论