Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Dobedani - 06 Jul 2005 13:35 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.