15

How do I get all the parameterNames in an HTML form in the same sequence?

Example:

  • If the form contains FirstName, LastNameand Age

  • The output should appear exatcly in the same sequence

I have tried using the following but this shifts the order of the output:

Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
    String paramName = (String) paramNames.nextElement();
    out.print(paramName);
}
ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
jcdmb
  • 3,016
  • 5
  • 38
  • 53
  • Hmmm.. if you explain your requirement, you will get a better solutions. You can always pass your ordering as a seperate form parameter. request.setParameter("form_ordering", data) or if the form ordering is relatively stable externalize the form details in a property file or use ajax to talk to server. Ajax libraries can convert your form values to Json and usually the operation is serial. – uncaught_exceptions Jan 19 '11 at 09:32
  • 1
    See also http://stackoverflow.com/questions/2317281/ordering-of-values-in-httpservletrequest-getparametervalues – skaffman Mar 28 '12 at 12:39

5 Answers5

8

I don't think there's nothing in the HTTP spec that forces browsers to send parameters in the order they appear in the form. You can work it around by prefixing a number to the name of the parameter like:

FirstName --> 0_FirstName
LastName --> 1_LastName
...

After that you could basically order the elements by the prefix. It is an ugly solution but it is the only way to do it. Something like:

// Assuming you fill listOfParameters with all the parameters
Collections.sort(listOfParameters, new Comparator<String>() {
    int compare(String a,String b) {
        return Integer.getInt(a.substring(0,a.indexOf("_"))) - 
               Integer.getInt(a.substring(0,b.indexOf("_")))
    }
});
for (String param : listOfParameters) {
    // traverse in order of the prefix
}

By the way - does it really matters the order in which you receive the parameters ?

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
Manuel Salvadores
  • 16,287
  • 5
  • 37
  • 56
  • I did not want to sort the parameters, but to retrieve them in the order I added them to the request. But thanks for answering – jcdmb Jan 20 '11 at 10:35
  • 1
    That's the thing there's no implicit order for parameters in the HTTP request. If you want to keep the order you have to establish that order in the way you name the parameters and then sort them when you get them in the server-side. So basically this is the only way to retrieve them in order. – Manuel Salvadores Jan 20 '11 at 10:51
3

None of the answers here really did answer my question. A HttpServletRequest saves all it's parameters in a HashMap, and a HashMap has NO ORDER. So, I saved the order of the parameters in an ordered ArrayList and saved it in a HttpSession, so I could retrieve the order of the parameters by querying the ArrayList (that was saved in the session) and achieve what I wanted!

jcdmb
  • 3,016
  • 5
  • 38
  • 53
  • 2
    That's already answered by msalvadores. Why are you replicating his answer? Just mark his answer accepted to indicate that it solved the problem for you. – BalusC Jan 20 '11 at 12:38
  • He probably used `request.getQueryString()` and manually splitted and stored the parameters in the session. If that's the case, msalvadores answer is not the same as his "solution", because he wouldn't need to specify custom parameter names. – Fagner Brack Nov 21 '14 at 01:12
2

request.getParameterNames () uses HashMap internally to store the name value pairs of form fields. There is no order maintained in this. if you need this in order then , some sort of naming convention for form parameters to control the order of retrieval.

SortedSet temp = new SortedSet();
Enumeration enumeration = request.getParameterNames();
while (enumeration.hasMoreElements()) 
{
        temp.add((String)enumeration.nextElement());
}
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
  • 1
    I did not want to sort the parameters, but to retrieve them in the order I added them to the request. But thanks for answering. – jcdmb Jan 20 '11 at 10:35
2

Updated: You can use sorted set for that. Note that you must have all the parameters with different names (in this case it is most likely). Write any prefix as your parameter name.

Example:

<input type="text" name="1step">
<input type="text" name="2step">

Then in java code you can write:

SortedSet ss = new TreeSet();
Enumeration<String> enm = request.getParameterNames();
while(enm.hasMoreElements()) {
    String pname = enm.nextElement();
}
Iterator i = ss.iterator();
while(i.hasNext()) {
    String param = (String)i.next();
    String value = request.getParameter(param);
}
    
ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
MKDoshi
  • 57
  • 6
0

HTML or Jsp Page

<input type="text" name="1UserName">
<input type="text" name="2Password">
<input type="text" name="3MobileNo">
<input type="text" name="4country">

and so on...

then in java code

SortedSet ss = new TreeSet();
Enumeration<String> enm=request.getParameterNames();
while(enm.hasMoreElements()){
    String pname = enm.nextElement();
    ss.add(pname);
}
Iterator i=ss.iterator();
while(i.hasNext()) {
    String param=(String)i.next();
    String value=request.getParameter(param);
}
HpTerm
  • 8,151
  • 12
  • 51
  • 67
Nilesh
  • 16
  • 1