仅检索POST参数(Java) [英] Retrieve POST parameters only (Java)
问题描述
有没有人知道从HttpServletRequest对象获取POST参数的方法?
Does anyone know of a way to get only POST parameters from an HttpServletRequest object?
IE,PHP有$ _POST超全局,而Perl的CGI.pm只会检索如果HTTP方法是POST(默认情况下),则为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).
HttpServletRequest.getParameter(String)将包含 GET URL参数,即使HTTP方法是POST。
HttpServletRequest.getParameter(String) will include the GET URL parameters even if the HTTP method is POST.
推荐答案
我想一种方法可能是手动解析 HttpServletRequest.getQueryString()
并检查其中是否存在参数。
I guess one way might be to manually parse HttpServletRequest.getQueryString()
and check that a parameter is not present in it.
一个简单的实现(忽略url-escaped键值)会像这样(未经测试):
A naive implementation (ignoring url-escaped key values) would go something like this (untested) :
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;
}
这篇关于仅检索POST参数(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!