1

1.here is my form code:

<form method="GET" action="Add">
first-name: <input type="text" name="first-name" value=""/><br/>
last-name: <input type="text" name="last-name" value=""/><br/>
email-id:  <input type="text" name="email" value=""/><br/>
<input type="submit"  name="submit" value="join now"/><br/>
</form>

2.here is servlet fragment:

Enumeration<String> en=request.getParameterNames();

    while(en.hasMoreElements()){
        String param=en.nextElement();
        PrintWriter pw=response.getWriter();
        pw.print(param);
        pw.println(request.getParameter(param));

here is output:

first-namevishal
emaildrunkendeathison@gmail.com
submitjoin now
last-nameanand

why is it not fetched in order? after first-name last-name must come and then email and submit must come, right??

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
Vishal Anand
  • 581
  • 3
  • 10
  • 22

2 Answers2

1

The parameters are stored in a Map (Most likely a HashMap) and there is no guarantee of order of items in a HshMap. That is items are ordered by the Hash of their keys. When you ask for the Enumeration of the names of the parameters you may not get them returned by the order in which they were inserted into the map. You should not depend on this order in your servlet.

There are some discussion about the order of items in a HashMap in this question.

Community
  • 1
  • 1
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
1

This isnt something specific to servlet. All server side technologies will behave the same way as the html form data consists of key-value pair. If you have any logic which expects the order to come, you may need to relook at it. Generally it doesn't make any difference in what order you receive the data, what you receive is more important.

CuriousMind
  • 3,143
  • 3
  • 29
  • 54