How to get values of getParameterValues in the order they are sent?

2.7k views Asked by At

I am sending registration form data through HttpPost method to Servlet and get this data by getParameterValues.

No problem with getting data, but I get the data in random order. I want that at servlet I get the data in order of how they are sent. I try to solve this by reading in internet but nothing can help. I am posting my servlet code here.

response.setContentType("text/html");
    ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
    Enumeration paramNames = request.getParameterNames();
    String params[] = new String[7];
    int i=0;

    while(paramNames.hasMoreElements())
    {
        String paramName = (String) paramNames.nextElement();
        System.out.println(paramName);


        String[] paramValues = request.getParameterValues(paramName);
        params[i] = paramValues[0];

        System.out.println(params[i]);

        i++;
    }

I get the output like this

5_Country
United States
4_Password
zxcbbnm
1_Lastname
xyz
0_Firstname
abc
3_Email
[email protected]
6_Mobile
1471471471
2_Username
abcd

I want 0_Firstname first then 1_Lastname then 2_Username like that, because I want to insert this data in database. Here 0,1,2...I wrote just for indicate that I want value in this order.

2

There are 2 answers

2
Anurag Anand On BEST ANSWER

You won't get the parameter names in order by using request.getParameterNames(); .

You can either use

String [] parameterNames =  new String[]{"param1","param2","param3"};

for(String param : parameterNames){
 System.out.println(param);
}

where parameterNames conains sequence in which you want your parameters. You can even configure it and read sequence from a config file.

OR

You can use

 request.getQueryString() to get the QueryString, while using GET Method

OR

You can use

 request.getInputStream() to get the QueryString, while using POST Method
 and parse the raw data to get the Query string.

After getting query string , you can split and use the way you want.

2
Sivakumar On

Try this

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

Convert Enumeration to List inorder to sort them.

List<String> listParamNames = Collections.list(enumParamNames);

The paramNames will look like this before sorting

[5_Country, 4_Password, 1_Lastname, 0_Firstname, 2_Username, 3_Email]

Sort the list by Collections.sort(listParamNames);

The sorted paramNames will look like this

[0_Firstname, 1_Lastname, 2_Username, 3_Email, 4_Password, 5_Country]

Now you can loop with listParamNames to get the associated param value

for(String paramName : listParamNames)
{
    System.out.println(paramName);
    System.out.print("\t");

    /* Instead of using getParameterValues() which will get you String array, in your case no need for that. You need only one `Value`, so you go with `getParameter` */
    System.out.print(request.getParameter(paramName));
}

Output:

0_Firstname - abc

1_Lastname - xyz

etc....