MetaChrome
MetaChrome

Reputation: 3220

Associative array request parameter parsing with Java Servlet

Is it possible to parse the string keys from a request such as this, with the Java servlet API?

http://localhost:8080/?assocArray[key1]=value1&assocArray[key2]=value2&assocArray[key3]=value3

getParameterValues("assocArray") returns ["value3","value1","value1"]

The ordering of the values in the returned array is not order of the keys (not that it matters)

SOLVED: It is possible, the keys are interpreted as simple global key strings. Java doesn't recognize them as an array. Use regex

Upvotes: 7

Views: 13054

Answers (4)

Stephen C
Stephen C

Reputation: 718836

SOLVED: It is possible, the keys are interpreted as simple global key strings. Java doesn't recognize them as an array. Use regex

Java doesn't recognize them as (elements of) an array because the URL / URI specs don't say that they represent an array. You shouldn't expect Java (or other) libraries to implement custom syntaxes that you've essentially pulled out of the air.

Using regexes for parsing input is generally speaking a bad idea. For (complete) URLs it is a bad idea because there are a variety of tricky way that URLs can be encoded, and it it hard for a regex to take account of this.

(If you are using a regex to split apart a single request parameter, you should be OK because the Servlet infrastructure will have already dealt with the primary URL parsing and dealt with %-encoding, etc.)


I'd also like to point out that your query String syntax is fragile. You are using the '[' and ']' characters without escaping them. According to the URL / URI specs they are "reserved characters" and should be escaped when used as "data". You will probably get away with it in most contexts, but applications that require strictly conformant URIs will reject your URLs.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691765

You may have several values for the same parameter name:

http://localhost:8080/?param1=value1&param1=value2&param1=value3

In this case, getParameterValues("param1") will return a String[] containing 3 elements : "value1", "value2" and "value3".

In the example you gave, you defined 3 different parameters : assocArray[key1], assocArray[key2] and assocArray[key3]. The servlet API will consider them as 3 totally different parameters having nothing in common. You thus have to call getParameter("assocArray[key1]") to get "value1", getParameter("assocArray[key2]") to get "value2", etc.

Upvotes: 19

BalusC
BalusC

Reputation: 1108742

Not directly. The [] have no special meaning in HTTP request parameters and are by the Servlet API not recognized as array keys (you was perhaps a PHP programmer which indeed has a proprietary parser for this?). You need to parse and collect it yourself in a loop.

For example,

Map<String, String> assocArray = new LinkedHashMap<String, String>();

for (Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
    String name = entry.getKey();

    if (name.startsWith("assocArray[")) {
        String key = name.substring(name.indexOf('[') + 1, name.indexOf(']')); 
        assocArray.put(key, entry.getValue()[0]);
    }
}

Upvotes: 11

Bozho
Bozho

Reputation: 597116

Not possible, AFAIK. The http protocol specifies key & value only, no key[key]=value

Use the string array by index - values[0], values[1], etc.

You can make some preprocessing - use request.getParameterMap(..), parse the foo[bar] syntax yourself, and put it in a Map<String, String>

Upvotes: 1

Related Questions