开发者

Receive Linked List from client using Java GET

I'm receiving string from client like following -

String time_S = request.getParameter(Message.KEY_TIME);

Now, If I want to receive a linked list data how should I do that? I tried to use getParameter开发者_如何学编程Values but I don't think I could use it properly.

Thanks in advance.


You can't really retrieve a 'linked list' per se over HTTP - it needs to be serialized (transformed from a Java object to a string). There are plenty of ways of doing this, but you might have them send it to you as a set of comma separated values and then parse it into a linked list or java data structure of your choice.


If you have them send if via JSON, there are several libraries that can be used which will change them to Java standard objects. Such as Simple.JSON, it turns JSON Array's into Java List objects, or JSON Object's into Java Map's.


getParameterValues might be the easiest way, provided that the client can provide each element of the list as a repeated querystring parameter.

For example if the client can send this querystring:

color=red&color=white&color=blue

getParameterValues("color") will return {"red", "white", "blue"}


Depends on how you submit the data. For example, if you submitting data from a web page and you are submitting data using the same parameter name, you can use the getParameterValues method.

For example, take the following inputs:

<input type="text" name="time" />
<input type="text" name="time" />
<input type="text" name="time" />

Then you can access the parameters as follows

String[] times = request.getParameterValues("time");

And if you need that linked list, just do the following

LinkedList<String> timeList = new LinkedList<String>(Arrays.asList(times));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜