Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

get request parameters are not in order

Thread view: 
John - 07 Jun 2006 00:53 GMT
Here's the code fragment to enumerate the request parameters list, but
the order
is not the same as in the query string.

http://localhost:9999/projWeb/EStoreServlet?a=1&b=2&c=3&d=4

==========================
Code
==========================
for (Enumeration e = req.getParameterNames(); e.hasMoreElements();)
{    Object obj = e.nextElement();
    String key = obj.toString();
    String value = req.getParameter(key);
    out.println(key + "," + value + "<br>");
}

==========================
output
===========================
b,2
a,1
sub,Logon
d,4
c,3

any ideas?

please advise. thanks!!
John.
Matt Humphrey - 07 Jun 2006 13:28 GMT
> Here's the code fragment to enumerate the request parameters list, but
> the order
> is not the same as in the query string.
>
> http://localhost:9999/projWeb/EStoreServlet?a=1&b=2&c=3&d=4

<snip demonstration code>

The parameters are name-value pairs without any presumption of order. They
are probably stored in a HashMap and the enumeration order depends on the
hashing.  I tried your code and the parameters came back in mixed order for
JBoss 4.0.2.

I searched through various javadocs and could not find anything that says
the parameters should be retrievable in their original order.  If your
design relies on parameter order, something is going to have to change.
Also, I don't think there's any guarantee that forms or other external
sources will put the parameters in the textual order given, or that URL
transforms (URL rewriting) will preserve the order.

If you can't change the design, you can retrieve the original query string
(getQueryString) and pick off the parameters yourself.  There are some
libraries to do this, although I don't know their names. Especially for POST
requests, reading the body as a stream may interfere with the ability to
read the names in order.

Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/ 
Andy Flowers - 07 Jun 2006 19:10 GMT
> Here's the code fragment to enumerate the request parameters list, but
> the order
> is not the same as in the query string.

<snip>

> any ideas?
>
> please advise. thanks!!
> John.

The order is not guaranteed and can be affected by the the sending client and
the receiving server.

Why do you need to read the parameters in a specific order ?

If you need to do that then you will have to do the ordering yourself, possibly
by appending a number to the parameter name to give the order,

i.e.

http://localhost:9999/projWeb/EStoreServlet?param1=1&param2=2&param3=3&param4=4

and then parsing this using the know prefix 'param'.
Owen Jacobson - 08 Jun 2006 03:34 GMT
>> Here's the code fragment to enumerate the request parameters list, but
>> the order
[quoted text clipped - 9 lines]
> The order is not guaranteed and can be affected by the the sending client and
> the receiving server.

No, only by the receiving server.  According to the HTTP and URL/URI
specifications the URLs

 http://www.example.com/someapp/servlet?a=foo&b=bar
and
 http://www.example.com/someapp/servlet?b=bar&a=foo

are distinct resources.  The Java servlet specification allows the
servlet container to reorder request parameters because in practice those
are more often the same resource with two names than two distinct
resources, and storing the parsed parameters in a hashmap is far faster
than seeking through them in order.

IMO parameters should probably be provided in a LinkedMap or LinkedHashMap
stored in URL order, because there are some useful idioms based on
multiple occurrences of the same parameter, but I haven't written a
servlet container or plugin, so my opinion's worth the electrons to print
it and not a bit more.

Owen
Henry Townsend - 08 Jun 2006 17:47 GMT
> IMO parameters should probably be provided in a LinkedMap or LinkedHashMap
> stored in URL order, because there are some useful idioms based on
> multiple occurrences of the same parameter, but I haven't written a
> servlet container or plugin, so my opinion's worth the electrons to print
> it and not a bit more.

At least in Tomcat my experience has been that repeated parameters are
always returned in order. I.e. using getParametervalues() with:

    http://www.example.com/someapp/servlet?a=X&a=Y&x=Z

returns an array in the "natural" order. I agree this is a useful idiom
and could be a solution for the OP, but I don't see it guaranteed
anywhere in the spec.

HT


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.