开发者

Order of request.getParameterNames()

开发者 https://www.devze.com 2023-02-05 16:43 出处:网络
How do I get all the parameterNames in an HTML form in the same sequence? Example: If the form contains FirstName, LastNamean开发者_如何学编程d Age

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

Example:

  • If the form contains FirstName, LastNamean开发者_如何学编程d 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);
}


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 ?


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!


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号