仅检索 POST 参数 (Java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1197729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve POST parameters only (Java)
提问by dankuck
Does anyone know of a way to get only POST parameters from an HttpServletRequest object?
有谁知道只从 HttpServletRequest 对象获取 POST 参数的方法?
IE, PHP has the $_POST superglobal and Perl's CGI.pm will only retrieve POST parameters if the HTTP method is POST (by default).
IE,PHP 有 $_POST 超全局变量,如果 HTTP 方法是 POST(默认情况下),Perl 的 CGI.pm 只会检索 POST 参数。
HttpServletRequest.getParameter(String) will include the GETURL parameters even if the HTTP method is POST.
HttpServletRequest.getParameter(String) 将包括 得到URL 参数,即使 HTTP 方法是 POST。
采纳答案by ykaganovich
I guess one way might be to manually parse HttpServletRequest.getQueryString()
and check that a parameter is not present in it.
我想一种方法可能是手动解析HttpServletRequest.getQueryString()
并检查参数中是否不存在。
A naive implementation (ignoring url-escaped key values) would go something like this (untested) :
一个幼稚的实现(忽略 url 转义的键值)会是这样的(未经测试):
public boolean isInQuery(HttpServletRequest request, String key) {
String query = request.getQueryString();
String[] nameValuePairs = query.split("&");
for(String nameValuePair: nameValuePairs) {
if(nameValuePair.startsWith(key + "=")) {
return true;
}
}
return false;
}
回答by df.
From my understanding, there are no such things as POST parameters and GET parameters in HTTP, there are POST and GET methods. When a request is made using the POST method, parameters go within the message body. In case of a GET request, parameters go in the URL.
根据我的理解,HTTP 中没有 POST 参数和 GET 参数之类的东西,只有 POST 和 GET 方法。当使用 POST 方法发出请求时,参数将位于消息正文中。在 GET 请求的情况下,参数进入 URL。
My first thought was that it was an implementation bug in your servlet container. But, since things are not always as you expect, java servlet specification (at least the 2.4 version) does not differentiate between the two kind of parameters. So, there is no way to obtain post or url parameters using the servlet API.
我的第一个想法是这是您的 servlet 容器中的一个实现错误。但是,由于事情并不总是如您所料,java servlet 规范(至少 2.4 版本)没有区分这两种参数。因此,无法使用 servlet API 获取 post 或 url 参数。
Surely you already have a plan B. But, just in case, I post two alternatives that came to my mind:
当然,你已经有了 B 计划。但是,为了以防万一,我发布了两个我想到的替代方案:
If you have access to the parameter name definition, you could use a prefix to differentiate between the two when you iterate the getParameterNames() result.
You could parse the URL creating an URL object and using getQuery() method to obtain just the parameters. Then, parse the parameters on the query string using some utility class such as ParameterParserin HttpClientlibrary. And finally, subtract that names from the getParameterNames() result.
如果您有权访问参数名称定义,则可以在迭代 getParameterNames() 结果时使用前缀来区分两者。
您可以解析 URL 创建 URL 对象并使用 getQuery() 方法仅获取参数。然后,解析使用一些工具类的查询字符串参数,如ParameterParser在 HttpClient的库。最后,从 getParameterNames() 结果中减去这些名称。
回答by seth
Couldn't you just get the parameters from the HttpServletRequest within doPost
or doGet
in a subclass of HttpServlet
?
你不能从 HttpServletRequest 的内部doPost
或doGet
子类中获取参数HttpServlet
吗?
Anything you grab (via getParemeter
) inside of doPost is a POST and anything inside of doGet is a GET.
您getParemeter
在 doPost 中抓取(通过)的任何内容都是 POST,而 doGet 中的任何内容都是 GET。
回答by skaffman
I'm not sure if this would work, but you could try extracting the raw content of the POST body using request.getReader()
. The container may remove that data before handing control to your application, though, and even if it didn't, you'd have to decode the parameter string yourself.
我不确定这是否可行,但您可以尝试使用request.getReader()
. 但是,容器可能会在将控制权交给您的应用程序之前删除该数据,即使没有,您也必须自己解码参数字符串。
回答by skaffman
I think you could do something with getMethod() available from the HttpServletRequest Interface.
我认为您可以使用 HttpServletRequest 接口提供的 getMethod() 做一些事情。
This is also available in 1.4 and 1.5.
这在 1.4 和 1.5 中也可用。
回答by AlcaDotS
The question was answered in this related post:
这个问题在这个相关帖子中得到了回答:
Normaly you can GET and POST parameters in a servlet the same way:
request.getParameter("cmd");
But only if the POST data is encoded as key-value pairs of content type: "application/x-www-form-urlencoded"like when you use a standard HTML form.
通常,您可以以相同的方式在 servlet 中 GET 和 POST 参数:
request.getParameter("cmd");
但仅当 POST 数据被编码为内容类型的键值对时:“application/x-www-form-urlencoded”就像您使用标准 HTML 表单一样。