Order of Parameters

ArrayList names, values;

names =new ArrayList();

values =new ArrayList();

// Find out the names of all the parameters.

java.util.Enumeration params = request.getParameterNames();

while (params.hasMoreElements())

{

// Get the next parameter name.

String paramName = (String) params.nextElement();

// Use getParameterValues in case there are multiple values.

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

names.add(paramName);

values.add(paramValues[0]);

}

This code will be used to handle a wide variety of forms, so it needs to continue to be dynamic. However, right now when I pass it parameters, it returns them in aphabetical order rather than in the order I passed them. For example, if I pass the following name:value pairs to the JSP:

name : John Smith

address : 123 Notreal Place; Somewhereville, PA 12345

company : SomeCo

I get back:

address : 123 Notreal Place; Somewhereville, PA 12345

company : SomeCo

name : John Smith

I need these to in their original order. Is there a way to maintain the orignal order or to find out what the original order was so that I can re-order the parameters?

Message was edited by:

DavidKerk

[1545 byte] By [DavidKerka] at [2007-11-26 18:42:44]
# 1
I don't think one can control the order of parameters.Why do you need them ordered in the original way? Please explain with code if possible, we might be able to suggest alternative ways of implementing whatever you want to achieve with that code.
appy77a at 2007-7-9 6:16:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I need them in order because I am going to use them to generate emails that will be sent to our secretaries. Since the secretaries need this information to fill out registrations for meetings, it will make their jobs a lot more difficult if the emails are ordered differently from the forms to which they correspond.

DavidKerka at 2007-7-9 6:16:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

One way to implement the order you want the contents to appear would be to use a "sort order bit" .

So the solution is to create an object that stores an e-mail address along with the desired sort-order, and store instance of that object in an ArrayList.

This way no matter how the parameters are ordered internally by ArrayList, you would always have control over the sort order.

When reading the arraylist, simply order the contents by the pre-defined sort order.

So for example if the ArrayList has something like

XYZ 1

DRE 2

ABC 3

4der 4

Regardless of their alphabetical order, there's a sort order bit on the right that pre-defines the order in which they should be read.

Message was edited by:

appy77

appy77a at 2007-7-9 6:16:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hm. That would entail adding the sort order byte to the forms that this JSP is supposed to handle, right? If so, it's not ideal because this JSP will be handling 100+ forms which are already written. I'll talk to the guys in my department and see if that's acceptable.

The JSP is supposed to replace a Perl script that used to handle the forms, and that script maintained the order without any special code. I wonder; why doesn't JSP receive the parameters in order while Perl does?

DavidKerka at 2007-7-9 6:16:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

I don't literally mean a sort order bit.

If I translate what I was saying into Object Oriented langauge it would be something like.

Say you want to pass a list of E-Mail addresses in a particular order, then create 1 Java Object that represents E-Mail

package test47;

public class Email {

private String emailAddress;

private int sortOrder;

public String getEmailAddress() {

return emailAddress;

}

public void setEmailAddress(String emailAddress) {

this.emailAddress = emailAddress;

}

public int getSortOrder() {

return sortOrder;

}

public void setSortOrder(int sortOrder) {

this.sortOrder = sortOrder;

}

}

In the ArrayList, instead of directly storing the E-Mail address as string, store the above Email object.

While you are storing the Email object into the ArrayList , also store the

sortOrder.

You need to test this out (with a small sample) to see if it works out the way you want, before going ahead to implement it full scale.

Then, while reading the ArrayList, use an Object Comparator and sort the items in the ArrayList by the sort order field.

You may or may not have to modify the HTML form, if when you submit the form the parameters on submit are ordered based on the order of entry. but I haven't tested this out.

There may be other ways to implement this, but this is all I can think of for now.

I haven't used Perl, so I can't really tell why the order of parameters is preserved in Perl and not in an ArrayList in Java.

appy77a at 2007-7-9 6:16:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...