Koerr
Koerr

Reputation: 15733

Is there a way to get POST parameters by original order from JSP/Servlet?

<form method="post" action="?">
    <input type="text" name="d" value="3">
    <input type="text" name="e" value="5">
    <input type="text" name="c" value="1">
    <input type="text" name="a" value="4">
    <input type="text" name="b" value="2">
    <input type="submit">
</form>

Processing POST request by:

Enumeration e = request.getParameterNames();
while(e.hasMoreElements()){
    out.println(e.nextElement());
}

This is an Enumeration that contains the parameter names in an unspecified order

Is there a way to get original text soruce from the POST request?

I want to get parameters by original order like this (as Chrome Developer Tools showing):

d=3&e=5&c=1&a=4&b=2

btw: I tried request.getQueryString() just return the query from URL(GET method), can't get any parameters from POST method.

Upvotes: 4

Views: 836

Answers (4)

Daniel
Daniel

Reputation: 353

ServletRequest.getInputStream() returns the raw input stream, but you need to use correct character encoding to build the post body. e.g,

r = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf8"));
StringBuilder sb = new StringBuilder();
String line;
while ( (line = r.readLine()) != null) sb.append(line);
System.out.println(sb.toString());

Upvotes: 2

Kailas
Kailas

Reputation: 827

On Submit of form you could call some function in javascript. Now this function will read data from page that user has entered. You can create post request and pass this parameters along in required order.

Upvotes: 0

atish shimpi
atish shimpi

Reputation: 5023

By using java script you form one veriable that will hold your parameters in sequence, if you want to separate it out by comma. and then pass those parameters to you controller. This approach provide you garenty to get the values in sequence.

Upvotes: 0

Sebastian P.
Sebastian P.

Reputation: 878

You could parse the URL by yourself. Just make sure that you store the results in a collection with predictable iteration order (e.g., LinkedHashMap).

Upvotes: 0

Related Questions