Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet parameter sequence?  RSS feed

 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've got a problem for which i need a solution very badly.
I have a html page with several fields. Let them be fld1,
fld 2, fld3.. etc. This is the order in which they appear in the page from top to bottom. In real scenario, they will be fields like name, city, address... etc. Please note that i have several pages like these with different contents.
Now i have a generic servlet in Tomcat server. This servlet needs to get the parameters sent to from the page in the same order it is sent. When i say same order , i mean the order like fld1, fld2, fld3.. etc. I can't hard code the order in the servlet coz there are several pages submitting to this servlet. Now my problem is that when i do req.getParameterNames(), it returns an enumeration . But this enumeration is not sorted. ie i don't get the parameters in the same order that i have sent.
Can anyone help me out
...thanks
..saj
 
Manjunath Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as i know there is no way to get the field values in a specified order....
But im interested to know why cant there be a good design to take care of
"This servlet needs to get the parameters sent to from the page in the same order it is sent... "
mpr
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure there's actually even a REQUIREMENT that a browser SEND fields in a particular order. It's always been a given that you get a list of name/value pairs and you are going to work on a name/value basis.
The one way you COULD do this is to get the actual URI string and parse it yourself, but, like I said, you're still assuming that all applicable browsers really work that way.
Alternatively, you could name every field the same name and get them via the array returned by getParameterValues()
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...or write the order you expect them as an extra, hidden field then use this ordering to define the order in which you read the rest of the fields...
Dave.
 
Manohar Karamballi
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Sajee!
This may bit turn round approach but it works with little sacrifice of code readability in HTML pages and Bit sorting overhead in Servlet.
What u can do is name ur fields in each page in alphabetical order. say u have fields name, age , address in ur HTML page in that order name them as
Aname,Bage,Caddress and comment why named them like that.
In Servlet u sort the Enumeration results and then start processing. Sice number of elements in any HTML page is less
(say hardly 20-25) sorting results in extra time in nano seocnds.
But this is Generic approach.
Any Comments?

Rgds
Manohar
 
jasonkosowan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it's just me, but you should never rely on parameter value ordering. If you really need them ordered, rake through the enumeration and slap the name-value pairs into a TreeMap. This way they will be (at least) ASCII-ordered when retrieved from the TreeMap. But it doesn't really answer WHY you would want to do this.
 
Alex Jalali
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a servlet that can handle forms. also the order of fields are accoring to what you specify.
See if you like using it
http://www.ubudesign.com/products/index.jhtml
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!