my dog learned polymorphism*
The moose likes Servlets and the fly likes retrieving request Parameters in order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "retrieving request Parameters in order" Watch "retrieving request Parameters in order" New topic
Author

retrieving request Parameters in order

Benjamin Weaver
Ranch Hand

Joined: Apr 08, 2003
Posts: 161
Hello all,

I am writing a web application. I want to retrieve and process html search field parameters in the same order the fields follow descending down the page.

How can I do this without a lot of sorting by alphabetically names, etc.? Does the Map returned by request.getParameterMap() offer some kind of ordering.?

Thanks in advance for any suggestions.
[ February 01, 2007: Message edited by: Bear Bibeault ]
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Use request.getRequestQUery and then parse it. It's fairly simple


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

Originally posted by D Rog:
Use request.getRequestQUery ...


There is no such method.

If you are referring to the query string, that would only work for GET requests, and nothing guarantees that they'll be in any specific order.

The bottom line is, you can't guarantee any order. You'll need to name the request parameters according to the order you want and sort them on the server. I've used naming such as: whatever[0], whatever[1], whatever[2] and so forth, and used a regular expression matcher on the server to retrieve the values in order.
[ February 01, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You might find that with a slightly different design, the requirement for processing them in some particular order goes away. Why does this requirement exist?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
Since the request holds parameters in a Map there is no guarantee of the order of keys.
The solution is to simply have a String[] of parameter names in the desired order in your servlet and iterate through it to retrieve the values.
Bill
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Originally posted by Bear Bibeault:


There is no such method.


Hmm, you can be right here, but I remember it was a method to get a request query, I swear.

Originally posted by Bear Bibeault:


If you are referring to the query string, that would only work for GET requests, and nothing guarantees that they'll be in any specific order.

You bet.

Originally posted by Bear Bibeault:

The bottom line is, you can't guarantee any order. You'll need to name the request parameters according to the order you want and sort them on the server. I've used naming such as: whatever[0], whatever[1], whatever[2] and so forth, and used a regular expression matcher on the server to retrieve the values in order.


Why do you need to sort them? Just get in an order you want, getParameter('whatever[0]').
Benjamin Weaver
Ranch Hand

Joined: Apr 08, 2003
Posts: 161
[Sorry about the double submission of the question--it was a slip]

Thanks, I came around to a similar solution, even though I don't like hard-coding this into a servlet. But it gets the job done. And yes, there is a request.getQueryString() method that returns the queryString from the URL. I could get the terms from there, I think. Another idea would be to make up a properties file with entries such as param1=title, param2=author and so on.

Why do I want to reconstruct the order? I would like to be told I don't have to, since it's a pain. But I don't see much of a way around it. The reason is because I allow the user the option of OR-ed and AND NOT searches on multiple search fields. Many databases online provide for OR-ing or AND NOT-ing of search criteria, but, it seems to me, few seem to be done correctly. They do not allow (and compell) the user to define the scope of the logical operators AND, AND-NOT, and OR.

For example,

"a AND (b or c)" yields not necessarily the same criterion as "(a AND b) or c" ;

"a AND b AND NOT (c or d)" might exclude more than ("a AND b AND NOT c) or d"


In the first example, the first expression yields, in the case of names where a="Smith" and b="Billings" and c="Jones",

"'Smith and Billings' or else 'Smith and Jones'"

whereas the second yields,

"'Smith and Billings' or else 'Jones'"


Only in the case of pure AND-ed or OR-ed searches do parens not have to be defined. For OR or AND-NOT the scope must be defined by the user. Nor is there, in these cases, any obvious default.

The user, it seems to me, must group his search terms by inserting parentheses around them on the search page.

Or so all of this seems to me. Input welcome!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
How -exactly- are you naming the various input fields of the submission form?

Bill
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Benjamin, I don't see what that long post has to do with having to order input fields. Unless perhaps you have a design with 20 separate input fields and you ask the user to put "Smith" in the first field, "and" in the second field, "Billing" in the third field, and so on. But I've never seen a search request page like that anywhere else. Normally the user just keys the whole string into one field and the server program parses it out. That's why I'm confused about what your post has to do with anything.
Benjamin Weaver
Ranch Hand

Joined: Apr 08, 2003
Posts: 161
I guess I could put it on the user to enter the logic. That would be the end of the story, I guess.

But in this database of ancient manuscripts we have a lot of search fields, each with several possible, and not-so-obvious values to choose from (writingHand = "Coptic Uncial", linguisticMedium = "hexameters"). We want to enable the user to enter concatenated search logic on EACH search field, in combination with similar search-logic entered on other fields.

There indeed will, in addition, be available a much simpler, google-style full-text search-page. But for complex searches there will also remain an advanced searchpage displaying approximately 60 search fields.

How much simpler and faster it seems to me in this case if the user is provided a search page, containing a number of search fields down the page, many with drop-down menus indicating elegible values, with buttons enabling insertion of parentheses to group fields in searches.

In any case, simply put, a user will do a search like "find all books where (author=Jones or author=Smith) and title=Wars and writingHand=Coptic Uncial" and so on.

If all this seems irrelevant, this item can be laid to rest. Thanks all for the information about representing the order of parameters on a searchpage!
Benjamin Weaver
Ranch Hand

Joined: Apr 08, 2003
Posts: 161
ps William and Paul, I see what you might be asking: why not just have say 4 fields and be done with it? Maybe on each row a Param field where the user selects "author" and then a "value" field where the user types in "Smith". Perhaps we could get away with this.

What I am trying to do, at least on the advanced page, is similar to the Advanced Search Page on www.amazon.com. (navigate to "Books", then to "Advanced Search Page"). Particularly relevant are the search fields on the bottom half of the page. Notice how (1) the search fields are each labeled; (2) the values selected by the user are not obvious (Format = Audio Download) so each, labeled field, has its own drop-down menu indicating possible values.
Benjamin Weaver
Ranch Hand

Joined: Apr 08, 2003
Posts: 161
Thanks all, once again, for your input. Paul and William, I am coming around to your position. Might do it just like Amazon does it, with an advanced query window at the bottom. The method I had proposed couples the query page with underlying business layers, which is of course not good. And the advantage supposedly gained--the visual display of parentheses on the search page--turns out not to be outstanding.

So you're right--I might as well do it like everyone else, letting the user compose more complex queries.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retrieving request Parameters in order