简体   繁体   English

有没有办法从JSP / Servlet按原始顺序获取POST参数?

[英]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: 处理POST请求:

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

This is an Enumeration that contains the parameter names in an unspecified order 这是一个Enumeration ,它包含未指定顺序的参数名称

Is there a way to get original text soruce from the POST request? 有没有办法从POST请求中获取原始文本?

I want to get parameters by original order like this (as Chrome Developer Tools showing): 我希望按原始订单获取参数(如Chrome Developer Tools所示):

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. 顺便说一句:我试过request.getQueryString()只是从URL返回查询(GET方法),无法从POST方法中获取任何参数。

ServletRequest.getInputStream() returns the raw input stream, but you need to use correct character encoding to build the post body. ServletRequest.getInputStream()返回原始输入流,但您需要使用正确的字符编码来构建帖子主体。 eg, 例如,

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());

You could parse the URL by yourself. 您可以自己解析URL。 Just make sure that you store the results in a collection with predictable iteration order (eg, LinkedHashMap). 只需确保将结果存储在具有可预测迭代顺序的集合中(例如,LinkedHashMap)。

By using java script you form one veriable that will hold your parameters in sequence, if you want to separate it out by comma. 通过使用java script您可以形成一个可以按顺序保存参数的verable,如果您想用逗号分隔它们。 and then pass those parameters to you controller. 然后将这些参数传递给您的控制器。 This approach provide you garenty to get the values in sequence. 这种方法可以让您按顺序获取值。

On Submit of form you could call some function in javascript. 在提交表单时,您可以在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. 您可以创建发布请求并按所需顺序传递此参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM