1

I have written a java servlet to deal with http get request.I know ,the common format of get request is like this:http://IP_ADDRESS:8080/test?name="jack"&value="shit.

But now ,I have a list of values to transfer,such as an user id list[1,2,3,4].So ,my question is ,how should I write my http get request to express this?And in java servets doGet(),can I use request.getParameterValues to get such an array?

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
wuchang
  • 3,003
  • 8
  • 42
  • 66

3 Answers3

3

if you are using GET method your url should be looking like that :

 http://IP_ADDRESS:8080/test?list=1&list=2&list=3

for retrieving it:

String[] arrlist=request.getParameterValues('list');

your array will be filled with separated values:

//["1","2","3"]

UPDATE : if to write it list[] or list?

when you retrieving your list parameters it wouldn't be parsed as array but as a series of String which will be grouped later on into an array. Which means even if you write it list[]=1&list[]=2&list[]=3, list[=1&list[=2&list[=3, list*=1&list*=2&list*=3 or list=1&list=2&list=3 it would always be giving you the same answer whether you retrieve it as

request.getParameterValues('list[]') //["1","2","3"]
request.getParameterValues('list[') //["1","2","3"]
request.getParameterValues('list*') //["1","2","3"]
request.getParameterValues('list')  //["1","2","3"]
Yehia Awad
  • 2,898
  • 1
  • 20
  • 31
0

While ,the http request format should be like this:localhost:8080/test?list[]=1&list[]=2&list[]=3

wuchang
  • 3,003
  • 8
  • 42
  • 66
  • you cannot do that ! unless you want your variable to be named as "list[]" look at my answer above – Yehia Awad Dec 30 '13 at 07:23
  • look at this page:http://stackoverflow.com/questions/3061273/send-an-array-with-an-http-get – wuchang Dec 30 '13 at 07:25
  • exactly like i have told you when you will be retrieving your list[] parameters it wouldn't be parsed as array but as a serie of String parameter which will be grouped later on into an array which mean even if you write it list[]=1&list[]=2&list[]=3 or list[=1&list[=2&list[=3 or list*=1&list*=2&list*=3 it would always give you the same answer if you retrieve it like that request.getParameterValues('list[]') request.getParameterValues('list[') request.getParameterValues('list*') – Yehia Awad Dec 30 '13 at 07:32
0

Maybe too simple, but what about repeat parameters name?

http://IP_ADDRESS:8080/test?userId=1&userId=2&userId=3
Diego Martelli
  • 302
  • 1
  • 10