Order of the data posted by form
hi guys.
i am working on servlets and gets the form values posted from an html form.
i can get all the form fields and their values, even of multiple check boxes and radio buttons but the problem i am fasing is the order of the form field in which they r in html differs with the enumeration reurned by httpservletrequest object.
can any one help me how can i get the form field in the same order as they r on the html page ?
my code is as under
Enumeration enu_p = request.getParameterNames();
while(enu_p.hasMoreElements()){
name = (String)enu_p.nextElement();
value = request.getParameter(name);
System.out.println("Parameters " + name + " = " + value );
//-->
String [] str = request.getParameterValues(name);//for extracting multiple values by the same name
for(int i = 0; i < str.length; i++){
value = str;
queryString = queryString + "&" + name + "=" + value;
System.out.println("\t\t" + name + " = " + value );
}
}
thanx in advance
There is no way to do what you ask. The code needs to handle any order. If you really want to process in a set order, why not retrieve the form arguments by name instead of in an Enumeration?
Define a interface with a constant of all form field names in the order you want...
public interface xyz {
String[] formfields = {"form_field_name1","form_field_name_2","form_field_name......n};
}
Implement this interface in your servlets and loop through to find the values on all form field.
servlets implements xyz{
............
..........
String value;
for(int i=0; i< fomfields.length; i++){
value = getParameter(formfields);
}
}
thanx fellows for spending ur precious time.
here i have the problem that i have one servlet which handles more then 10 forms, so its difficult to keep track of each form field.
i think there should be a way to get the data posted in the same order as posted by browser
cause the browser send the data after the request header and now the SERVLET API is changing its order ?
e.g
normally the request is entertained in this order
client request -> <B>posted data</B> -> server response header -> data from server -> end
why servlet api is changing the order of the data posted by the client (browser) ?
i need help in this regard where from i can get the servlets implementations so i can change it directly or there would be any property through which i can mainain its orignal order
thanx guys
>here i have the problem that i have one servlet which handles more then 10 forms, so its difficult to keep track of each form field.
I don't see why that requires you to see the fields in any particular order. In fact I don't see any reason why you would really need that. Do you have a reason?
i am working on a project
there i want an intelligent servlet which gets the data from the forms and on the order based updates the database.
e.g we have a module where user places their classifieds,
and there are more then 20 categories in the classifieds and are growing .....
now its very difficult for me to create a new servlet for the new form. i only wana create new form which also is dynamically generated. and i am using this mechanism to collect the information fro user and then update the database and also show the data on the fields order.
the same field stores the multiple data
for one row it stores the color of a car, for some it stores the address of a shop and for other it can store the name of some product.
thats why i need this.
thanx
Why don't you hardcode the field names in the required order and keep that value in a hidden field in each and every form? For example, if form1.html is having fld1,fld3,and fld2, then include a hidden field in the form, <input type="hidden" name="fieldOrder" value="fld1,fld3,fld2">
Afetr submitting it to the server, you can get the order easily. What do you say?
This is just a work around.
Sudha
Hi,
check the snippet of code .
Hashtable table = parseMulti(boundary, request.getInputStream());
Vectorformelements = new Vector();
for (Enumeration fields = table.keys(); fields.hasMoreElements(); ) {
String name = (String)fields.nextElement();
if( !name.equals("fileFieldName")){// File field name.
formelements.addElement(name);
}
}
Get all the keys, then retrive all names into an object and get the values of the fields based on the field names.
It is not going to depend upon the order rather depends upon the field name( GrayMan ) which makes your code bit meaningful than the one which depends up on the order of the elements.
Thanks,
Sanath Kumar.
thanx
Sanath Kumar !
i think i am still unable to define the problem
let me explain it again
i have a web page which contains some text fields and hidden values
<form method=post action=myServlet>
1 < input type=text name=fld1>
2 < input type=text name=fld2>
3 < input type=text name=fld3>
4 < input type=hidden name=hid1 value=>
5 < input type=text name=fld4>
6 < input type=hidden name=hid2 value=>
7 < input type=hidden name=hid3 value=>
</form>
now user submit this form
and data comes to myServlet
now here i use this code to extract the data posted from previous form
Enumeration enu_p = request.getParameterNames();
String data = "";
while(enu_p.hasMoreElements()){
name = (String)enu_p.nextElement();
value = request.getParameter(name);
String [] str = request.getParameterValues(name);//for extracting multiple values by the same name
for(int i = 0; i < str.length; i++){
value = str;
data = data + " + " + name + "=" + value;
System.out.println("\t\t" + name + " = " + value );
}
}
here i get the data in the string data, but it is not in the order as the input types are on the html page they are scattered. and changes their places.
i want them in the same order as they are on page.
here i have not the info which form should be submitted cause there are alot of forms, so its difficult for me to name the fields in a specific order. thats why i am looking for help
is there any why such that i can get the data directly posted from the page. that could be html, jsp
thanx fellows
Reply 6 will certainly work for you. I have tested it. But I know it is a bit crude way of doing it.Sudha
Hi, did you solved that situation? On iplannet server or Server One version 6.0 you can read all the variables in order by today I installed version 6.1 and the variables has no order, I developed a routine similar to you. I need use that feature. What did you do?
maybe this helps
when you crate the page you know the name of the inputs, right?
well, if it is so you could do this:
make a hidden for each input, like this
< input type=text name="fld1">
<input type="hidden" name="h_1" value="fld1">
< input type=text name="fld2">
<input type="hidden" name="h_2" value="fld2">
< input type=text name="fld3">
<input type="hidden" name="h_3" value="fld3">
< input type=hidden name="hid1" value=>
<input type="hidden" name="h_4" value="hid1">
...
and then...
enu_p = request.getParameterNames();
for(int i=1; i<=enu_p.size()/2;i++){
name = request.getParameter("h_"+i);
value = request.getParameter(name);
System.out.println("\t\t" + name + " = " + value );
//check for nulls
}
is that clear enough?
Jorge