Solved

ServletRequest::getParameterMap()

Posted on 2004-08-06
31
970 Views
Last Modified: 2012-05-05
I am submitting a form to a servlet using method POST. My form has many parameters (hidden and visible). When I use request.getParameterMap() in the doPost() method in the servlet (assuming that request is an instance of HttpServletRequest), I get a java.util.Map of parameter names and values. Using a java.util.Iterator in the java.util.Set for the keySet in the Map, I can obtain the names of all the parameters passed by the form. Funny enough: they are not in the order they exist in the form. The same thing happens on doGet if I pass the parameters on the URL while calling the servlet. Therefore, if the call to the servlet is like ServletName?param1=value1&param2=value2&param3=value3..., the iteration through the keySet does not result param1, param2, param3,... in order but in a completely unpredictable order. Any idea how to fix this? Thanks a lot in advance.
0
Comment
Question by:xtremebytes2002
  • 8
  • 8
  • 7
  • +2
31 Comments
 
LVL 1

Author Comment

by:xtremebytes2002
ID: 11736031
And the same problem with the following code as well

        Enumeration itp = request.getParameterNames();
        while (itp.hasMoreElements()) {
            System.out.println((String) itp.nextElement()); //shows parameters in an unpredictable order!
        }

instead of using the Map.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11736033
Map has no order.  Nor does set.

There is no order specified by the HTTP specification for parameters, you have to accept them in any old order :-/
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11736043
The last way you posted is the usual way of listing them all...

But as you have said, there is no order
0
 
LVL 35

Expert Comment

by:girionis
ID: 11736087
If it is very important to have the exact order then you can rearrange them once you receive them on the server. The other thing you can do is to pass a hidden variable with all the parameters in order, separated by comma so you can deduct on the receiving end the order.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11736095
An enumeration or iterator would normally not return the values in any normal order. They perhaps use hash-codes and some more stuff to arrange the elements in some order.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11736122
BTW, what is the reason for getting them in order?

ServletName?param1=value1&param2=value2

is same as:

ServletName?param2=value2&param1=value1
0
 
LVL 1

Author Comment

by:xtremebytes2002
ID: 11736150
Thanks. "If it is very important to have the exact order then you can rearrange them once you receive them on the server" <- how? Although the next comment (hidden fields) explains it in some way? How do you make use of hash-codes to achieve this? The order of parameters is very important because it is used to generate a DOM tree, in which the order must conform to a XML schema, which is used by XSL to generate the form. That means that the form has the right order and the servlet has to get the parameter order correct.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11736227
why not call your parameters:

item1, item2, item3, etc

then get them using a loop:

int i = 1 ;
while( true )
{
    String value = request.getParameter( "item" + i ) ;
    if( value == null )
        break ;
    i++ ;
}
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11736299
>> item1, item2, item3, etc

Hmmm. Coding standards won't allow ;-) most organizations prefer specific prefixes for text-boxes, combo-boxes, etc. Besides the names should also meaningful otherwise it'll be a pain maintaining them when the number of parameters is large ;-)

>> How do you make use of hash-codes to achieve this?

You don't use it. Java does it internally while creating the enumeration and iterators. Don't worry about that. That's why you don't get your elements in the order.

>> If it is very important to have the exact order then you can rearrange them once you receive them on the server" <- how?

Keep a hidden variable like:

<input type="hidden" name="orderOfParameters" value="param1,param2,param3" />

In the doPost () method, get this parameter:

String sOrder = request.getParameter ( "orderOfParameters" ) ;
String[] params = sOrder.split ( "," ) ; // if you're on JDK 1.4 or above

Otherwise, use a StringTokenizer or something of that sort.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11736352
> Hmmm. Coding standards won't allow ;-) most organizations prefer specific prefixes for text-boxes,
> combo-boxes, etc. Besides the names should also meaningful otherwise it'll be a pain maintaining them
> when the number of parameters is large ;-)

And the HTML standard won't allow it to post the params with any order ;-)

Hehehe, and a "hidden" field with the fieldnames in order is a good idea, until you reach the limit for the length of a parameter under IE...
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11736371
>> until you reach the limit for the length of a parameter under IE...

Let's hope there aren't so many params in a single form (man, how'll the form look with so many controls ;-))

xtremebytes2002 ,

Approximately, how many parameters do you have?
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11736385
Tim, girinois, you saw the Q I posted?
0
 
LVL 1

Author Comment

by:xtremebytes2002
ID: 11736411
Thanks. Each parameter is an XPath location pointing to the exact location in the DOM tree where the parameter is to be added. An example form code is as follows.

<form action="/testservlet" method="post" name="testForm" id="testForm">
  <p>
    <input name="/firstElement/second[1]/third" type="text" />
  </p>
  <p>
    <input name="/firstElement/second[2]/third/fourth" type="text" />
  </p>
  <p>
    <input name="/firstElement/second[3]" type="text" />
  </p>
  <p>
    <input name="/firstElement/unknown/other" type="text" />
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>

The recieving servlet has a XPath parser to handle multiple occurances but obviously if /firstElement/second[3] is received before /firstElement/second[2]/third/fourth, then it won't work because the third occurance of 'second' cannot exist when the second occurance does not exist. The concept of the hidden field is interesting but have you thought of how long the value of orderOfParameters can be if the form contains about 27 to 30 fields on average (including hidden) each having moderately long XPaths?
0
 
LVL 35

Accepted Solution

by:
TimYates earned 125 total points
ID: 11736445
> Let's hope there aren't so many params in a single form

Or let's hope the parameter names aren't very long...

> each having moderately long XPaths

eeeek...

is there no way to put the index of the parameter into the parametername?

ie:

 <p>
    <input name="1/firstElement/second[1]/third" type="text" />
  </p>
  <p>
    <input name="2/firstElement/second[2]/third/fourth" type="text" />
  </p>
  <p>
    <input name="3/firstElement/second[3]" type="text" />
  </p>
  <p>
    <input name="4/firstElement/unknown/other" type="text" />
  </p>

then once you get the correct one, stript the number off the front of the parameter...
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11736479
<input type="text" name="/FE/s[1]/t" />

:-) ?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:xtremebytes2002
ID: 11736514
<p>
    <input name="1/firstElement/second[1]/third" type="text" />
  </p>
  <p>
    <input name="2/firstElement/second[2]/third/fourth" type="text" />
  </p>
  <p>
    <input name="3/firstElement/second[3]" type="text" />
  </p>
  <p>
    <input name="4/firstElement/unknown/other" type="text" />
  </p>

Exactly, this is what i was thinking... just out of curiosity, i was wondering if there's a better way out. Thanks. Don't worry about the look and feel of the form. I am not the UI designer :-P Further to that, if they have big schemas with a flat list of parameters, then it is not exactly our problem :-P I need the thing working with any form whatsoever. Thereby, may be I will use filters to delegate the form sorting mechanism before passing it to the final form handling servlet.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11736552
Don't forget to strip the numbers off the start ;-)

Luckily that first "/" assuming it is in all of the parameters, makes a good token to strip to :-)

Tim
0
 
LVL 1

Author Comment

by:xtremebytes2002
ID: 11736590
Thanks a lot. Should I assume that there is no better solution?
0
 
LVL 35

Expert Comment

by:girionis
ID: 11736729
Do the parameter names change dynamically or not? You could do:

static Vector v = new Vector(4);
v.addElement("/firstElement/second[1]/third");
v.addElement("/firstElement/second[2]/third/fourth");
v.addElement("/firstElement/second[3]");
v.addElement("/firstElement/unknown/other");

Vector parameterValues = new Vector(4);

 Enumeration itp = request.getParameterNames();
        while (itp.hasMoreElements()) {
            parameterValues.add(v.indexOf((String) itp.nextElement()), (String) itp.nextElement());
        }
0
 
LVL 35

Expert Comment

by:girionis
ID: 11736749
> Tim, girinois, you saw the Q I posted?

Which one? In Java TA?
0
 
LVL 1

Author Comment

by:xtremebytes2002
ID: 11736764
All the parameter names are dynamic. My servlet hasn't got a clue what could be passed!
0
 
LVL 1

Author Comment

by:xtremebytes2002
ID: 11736779
Although it knows that the parameter names will conform to the XPath style, otherwise, the XPath parser won't be called. That's a different issue. However, I cannot create a Vector because I still need to know in which order I have to add the items to the Vector. Any other suggestions? Thanks.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11736808
> Any other suggestions?

Can't think of anything...
0
 
LVL 35

Expert Comment

by:girionis
ID: 11736844
Only the hidden variable which will also be created dynamically:

<input type="hidden" name="paramOrder" value="/firstElement/second[1]/third, /firstElement/second[2]/third/fourth, /firstElement/second[3]..."/>

Vector v = new Vector();
String order = request.getParameter("paramOrder");
StringTokenizer st = new StringTokenizer(order, ",");
while (st.hasMoreTokens())
{
     v.addElement(st.nextToken());
}

Vector parameterValues = new Vector();

 Enumeration itp = request.getParameterNames();
        while (itp.hasMoreElements()) {
            parameterValues.add(v.indexOf((String) itp.nextElement()), (String) itp.nextElement());
        }
0
 
LVL 35

Expert Comment

by:girionis
ID: 11736852
Also strip the spaces from the value of the hidden variable.
0
 
LVL 1

Author Comment

by:xtremebytes2002
ID: 11736904
the hidden field might be a bit impractical because of the way the param names are... they are too long and too many
0
 
LVL 35

Expert Comment

by:girionis
ID: 11736966
Then the only way is the one suggested above. Replace the first string with a number.
0
 
LVL 1

Expert Comment

by:wolfc
ID: 11736976
According to the W3C spec, no ordering of the parameters is defined. So you should not rely on that behaviour.

http://www.w3.org/TR/html4/interact/forms.html#h-17.13.3.2
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11742305
>> Which one? In Java TA?

Yeah.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11742730
Can you post th elink here? I think I have missed it :(
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11742795
Its in my question history as 'Open' under Java programming TA - http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21080150.html
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 47
HashMap Vs TreeMap 12 49
groovy example issue 10 66
Java Jpanels and Jframe 8 20
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now