问题
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 Age . the output should appear 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);
}
回答1:
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 ?
回答2:
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!
回答3:
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());
}
回答4:
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);
}
回答5:
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);
}
来源:https://stackoverflow.com/questions/4733373/order-of-request-getparameternames