开发者

Parsing a requestUrl in Java

开发者 https://www.devze.com 2023-03-06 04:18 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消