0

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.com/?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

p27
  • 2,217
  • 1
  • 27
  • 55
Ashish Sharma
  • 1,597
  • 7
  • 24
  • 35
  • 2
    This may help, Ashish http://stackoverflow.com/questions/1667278/parsing-query-strings-in-java – Jim Blackler May 13 '11 at 12:38
  • 2
    See this question on parsing URLs is java, there are a number of possibilities: http://stackoverflow.com/questions/1667278/parsing-query-strings-in-java – Richard H May 13 '11 at 12:39

2 Answers2

1

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));  
    }  
insumity
  • 5,311
  • 8
  • 36
  • 64
PPs
  • 26
  • 1
  • This looks fine in this way I am able to modify the content of that URL .But how can I get again get the URL from this map with modified value – Ashish Sharma May 16 '11 at 06:42
0

invoke request.getParameterMap and iterate over the querystring keys and replace with what ever needed

Surya Chaitanya
  • 602
  • 4
  • 13