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

Discussion in 'Java' started by Dobedani, Jul 6, 2005.

  1. Dobedani

    Dobedani Guest

    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
    Dobedani, Jul 6, 2005
    #1
    1. Advertising

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

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

    --
    XML is the programmer's duct tape.
    Raymond DeCampo, Jul 6, 2005
    #2
    1. Advertising

  3. Dobedani

    Nag Guest

    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);
    }
    Nag, Jul 6, 2005
    #3
  4. Dobedani

    Dobedani Guest

    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
    Dobedani, Jul 7, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. **ham

    Can i do sth like that in .NET?

    **ham, Dec 1, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    650
  2. Ken Clark
    Replies:
    1
    Views:
    546
  3. Scott Harper

    Using getParameterMap with Tomcat

    Scott Harper, Mar 1, 2007, in forum: Java
    Replies:
    4
    Views:
    9,300
    Chris Uppal
    Mar 2, 2007
  4. Taoge

    Can key in sth in Dropdownlist control?

    Taoge, Aug 21, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    106
    Taoge
    Aug 21, 2006
  5. elodie

    $sth->errstr; and $sth->execute;

    elodie, Nov 7, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    202
    elodie
    Nov 7, 2011
Loading...

Share This Page