Order of request.getParameterNames()

后端 未结 5 1034
无人共我
无人共我 2021-01-04 04:36

How do I get all the parameterNames in an HTML form in the same sequence as they are in the form.

i.e if the form contains .... FirstName, LastName, MiddleName and

相关标签:
5条回答
  • 2021-01-04 04:49

    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 讨论(0)
  • 2021-01-04 04:52

    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());
    }
    
    0 讨论(0)
  • 2021-01-04 04:53

    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 ?

    0 讨论(0)
  • 2021-01-04 04:57

    Updated: U can user sorted set for that. note that u must have all the parameter with different name. (in this case it is most likely) Write any pref.ix as your parameter name for ex.

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

    and so on...

    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);
        }
    
    0 讨论(0)
  • 2021-01-04 04:58

    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!

    0 讨论(0)
提交回复
热议问题