Using request.getParameterMap() - is there sth. like x.getValue (key)?

D

Dobedani

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
 
R

Raymond DeCampo

Dobedani said:
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

Not realizing that the values in the map are arrays of Strings, i.e.
String[].

HTH,
Ray
 
N

Nag

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);
}
 
D

Dobedani

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top