Dear All,
Apparently, request.getParameterMap() is the way - since about 3 years
- to approach the parameters coming in via the querystring in the
context of a servlet or Java Server Page. Looks quite powerful, but
unfortunately, I don't understand how to use the result of this
function, which is of class Map.
I have already tried a number of things. I tried to use methods
keySet() and entrySet() to obtain a set of all the keys after which I
used iterators to go through and get the key-value pairs, e.g.:
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Map.Entry" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.Iterator" %>
<html>
...
<%
Map params = request.getParameterMap();
Set set = params.entrySet();
Iterator iter = set.iterator();
while (iter.hasNext()) {
Entry n = (Entry) iter.next();
out.println(n.getKey().toString(), n.getValue().toString());
}
%>
...
</html>
The problem is that I do not get the strings back which I included in
the querystring. Instead I get values like: [Ljava.lang.String;@10f
which are of no use to me. I rather like sets of class Attributes. At
least, that class has a method getValue(key)!
What am I doing wrong??? Please help!!! TIA
Dobedani
Wageningen
The Netherlands
Raymond DeCampo - 06 Jul 2005 19:12 GMT
> Dear All,
>
[quoted text clipped - 31 lines]
>
> What am I doing wrong??? Please help!!! TIA
Not realizing that the values in the map are arrays of Strings, i.e.
String[].
HTH,
Ray

Signature
XML is the programmer's duct tape.
Nag - 06 Jul 2005 20:00 GMT
you should do something like with your map:
the values are array of strings.
Map.Entry me = (Map.Entry)iterator.next();
out.println(me.getKey() + ": ");
String[] arr = (String[]) me.getValue();
for(int i=0;i<arr.length;i++){
out.println(arr[i]);
}
Dobedani - 07 Jul 2005 10:51 GMT
Dear Ray and Nag,
Thanks for the answers! Of course, a querystring can contain multiple
values per key! I got it working as I wanted it. I can now add values
to an object of class Attributes. I will paste my code below, for
whoever will also almost stumble over this problem, in the future. HTH
I added this "import" to what I already imported in my earlier message:
<%@ page import="java.util.jar.Attributes" %>
Then:
<%
Attributes attribs = new Attributes();
Map map = request.getParameterMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Entry n = (Entry)iter.next();
String key = n.getKey().toString();
String values[] = (String[]) n.getValue();
attribs.putValue(key,values[0].toString()); // Only 1 value is
considered here
}
%>
And finally:
<table>
<%
Set set = attribs.keySet();
Iterator k_iter = set.iterator();
while (k_iter.hasNext()) {
String key = k_iter.next().toString();
out.println("<tr><td>" + key + "</td><td>" +
attribs.getValue(key) + "</td></tr>");
}
%>
</table>
Kind regards,
Dobedani