2

I have an existing Java web application that has about 50 query parameters, similar to

http://localhost/myapp?a=1&b=2&c=3&d=4

and I extract values in my servlet using ServletRequest.getParameter, similar to

String a = req.getParameter("a");

Each of my existing parameters are single-valued. I now need to introduce a new parameter that is multi-valued. What is the best practice to do this with respect to the RESTful practices and the servlet specification?

I am considering the two options below, but if there's something better, please point it out.

Comma-Delimted Single Parameter

http://localhost/myapp?a=1&b=2&c=3&d=4&e=5,6,7,8

which would be encoded as

http://localhost/myapp?a=1&b=2&c=3&d=4&e=5%256%257%258

and be extracted as

String eStr = req.getParameter("e");
String[] e = eStr.split(",");

Multiple Parameters

http://localhost/myapp?a=1&b=2&c=3&d=4&e=5&e=6&e=7&e=8

which wouldn't need any special encoding and be extracted as

String[] e = req.getParameterValues("e");
dhalsim2
  • 936
  • 2
  • 12
  • 35
  • **UPDATE**: I liked the reasoning behind the using multiple parameters. It just seems to be more elegant for my parameter delimiters to be baked into HTTP instead of sitting on top of HTTP. Unfortunately, the client's JavaScript library couldn't support multiple parameters of the same key, so I ended up having to go with a delimited single parameter. I did convince the client developer that we shouldn't use comma in order to avoid the encoding. We ended up using tilde ~ instead. – dhalsim2 Jun 16 '14 at 17:39

2 Answers2

2

As per this question, technically there's no defined spec, so either would be technically correct, but there are 3 reasons I'd go with request.getParameterValues("…")

  1. Code re-use: Each piece of code you write is an opportunity to introduce bugs (i.e. in this case the parser to read from your CSV query string). Normally, introducing a library has a downside in that you may be introducing a weighty dependency for a small function, or you may not be clear exactly what the 3rd party is doing in their library. In this case, it's essentially a 1st party library, as it's the JavaEE implementation itself giving you the method! It also provides…

  2. Clarity of intent: Anyone reading your code afterwards will be familiar with the library method & knows straight away what you're doing.

  3. URL structure: Avoiding the delimiting character gives a URL that much more human-readable.

Since you mention being RESTful, I think the main thing here is to use query strings appropriately — avoid using them to serve static content pages on your site (page.html?id=12ndi4t!) — but a URL structure of, e.g. search.html?q=film&category=horror&category=comedy is nice & clear for a search page.


The downside of the getParameterValues(…) approach is that the order of the returned values is not defined, so if you can't depend on e=5&e=6&e=7 producing:

String[] e = request.getParameterValues("e");
e[0] //5
e[1] //6
e[2] //7

If this is particularly important, you may want to go with your first approach outlined above.

Community
  • 1
  • 1
anotherdave
  • 6,656
  • 4
  • 34
  • 65
  • Thanks for the excellent points. When I have used getParameterValues() in Tomcat, I've always had the values returned in the order that they are in the query string. I can't count on this? – dhalsim2 Jun 11 '14 at 19:46
  • @dhalsim2 I'm not sure which way this is implemented behind the scenes — it may be done in such a way that you *will* always retrieve them in order. It's not explicitly defined by the spec though, so basically the contract they're providing you via the API docs doesn't guarantee that it won't change in the future. – anotherdave Jun 11 '14 at 20:17
1

The best practice would be to use multiple parameters.

http://localhost/myapp?a=1&b=2&c=3&d=4&e=5&e=6&e=7&e=8

We can look at how browsers treat select and checkboxes as an example:

http://www.w3.org/TR/html401/interact/forms.html#successful-controls

Note that the spec gives significance to ordering - "The control names/values are listed in the order they appear in the document". So you can reasonably assume that order will be respected by the parsing library.

For your specific situation, in the Java world, frameworks like Spring MVC will understand the multiple parameter form and bind it to List or Arrays for you, which is another benefit to using multiple parameters.

In a more pragmatic world, the answer depends on how your service will be used. Using multiple parameters incurs an overhead since you're repeating the parameter names. If you're service is going to be called billions of times a day, the overhead adds up.

John Cheng
  • 571
  • 1
  • 4
  • 11