问题描述:

Assuming that I'm not concerned about the security aspect of this, a client computer calls the url:

myurl.com/file.ext?Param1=value&param2=value2&param3=value3

I then want to do a simple insert into a database

Insert into myDB(Col1, Col2, Col3)VALUES(Param1, Param2, Param3)

I will be doing my own char stripping on the input, I just can't find anything succinct on how to do this.

网友答案:

See URL.getQuery() It will return the query part of the url, and then use this method

URl u = new URL("myurl.com/file.ext?Param1=value&param2=value2&param3=value3");
Map<String, String> params = getQueryMap(u.getQuery());

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[] parts = param.split("=");
        String name = parts[0];  
        String value = parts[1];  
        map.put(name, value);  
    }  
    return map;  
}

now you'll have all the parameters in the map, accessible. Format and execute the query using JDBC.

网友答案:
    String s = "myurl.com/file.ext?Param1=value&param2=value2&param3=value3";
    int index = s.indexOf("Param1");
    String ss = s.substring(index);
    String[] arr = ss.split("&");
    for(int i=0; i < arr.length; i++){
        String[] value= arr[i].split("=");//value[i] will be value i+1th value
    }
相关阅读:
Top