如何确定参数是否已从 Java“发布”或“获取”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1009844/
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
How to determine if a parameter has been "posted" or "geted" from Java?
提问by opensas
In ASP, there's request.form
and request.queryString
attributes, but in Java. It seems like we have only one collection, which can be accessed via request.getParamaterMap
, getParametersNames
, getParameterValues
etc.
在 ASP 中,有request.form
和request.queryString
属性,但在 Java 中。好像我们只有一个集合,它可以通过以下方式访问request.getParamaterMap
,getParametersNames
,getParameterValues
等。
Is there some way to tell which values have been posted and which ones have been specified in the URL?
有什么方法可以判断哪些值已发布,哪些值已在 URL 中指定?
PS:
PS:
What I'm trying to achieve is to make a page that can handle the following situation
我想要实现的是制作一个可以处理以下情况的页面
- Read the variables that came from the querystring (get)
- Read a single post with a certain name ( "xml", for example ).
- If that post is missing, read the whole body (with the
request.getReader()
).
- 读取来自查询字符串的变量 (get)
- 阅读具有特定名称(例如“xml”)的单个帖子。
- 如果该帖子丢失,请阅读全文(带有
request.getReader()
)。
I'm using tomcat 6.
我正在使用 tomcat 6。
According to what I've seen so far, if I issue a request.getReader()
, posted values no longer appears in the getParamater
collection, nevertheless querystring parameters are still there.
根据我目前所见,如果我发出request.getReader()
,发布的值不再出现在getParamater
集合中,但查询字符串参数仍然存在。
On the other hand, if I issue any of the getParameters
methods, getReader
returns empty string.
另一方面,如果我发出任何getParameters
方法,则getReader
返回空字符串。
Seems like I can't have the cake and eat it too.
好像我不能吃蛋糕也不能吃。
so, I guess the solution is the folowwing:
所以,我想解决方案是以下方法:
- Read the body with
getReader
. - See if the xml post is there (downside, I have to manually parse the body.
- If it is, get the http message body and get rid of the "xml=" part.
- If it's not, well, just get the body.
- Read the querystring parameters through
request.getParameter
- 阅读正文
getReader
。 - 查看 xml 帖子是否在那里(缺点,我必须手动解析正文。
- 如果是,则获取 http 消息正文并去掉“xml=”部分。
- 如果不是,那么,只要得到身体。
- 通过读取查询字符串参数
request.getParameter
Any better idea?
有什么更好的主意吗?
- PS: Does anybody knows how to parse the body using the same method
used by
HttpServlet
? - PS: Hereis a decoding ASP function. Shall I just rewrite it in Java?
- PS: Also found(don't have a machine to test it right now)
Just to clarify things. The problem seems to be that with getParameter
you get posted values as well as values passed with the URL, consider the following example:
只是为了澄清事情。问题似乎在于getParameter
您获得了发布的值以及通过 URL 传递的值,请考虑以下示例:
<%@page import="java.util.*"%>
<%
Integer i;
String name;
String [] values;
for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
name = (String) e.nextElement();
values = request.getParameterValues( name );
for ( i=0; i < values.length; i ++ ) {
out.println( name + ":" + values[i] + "<br/>" );
}
}
%>
<html>
<head><title>param test</title>
</head>
<body>
<form method="post" action="http://localhost:8080/jsp_debug/param_test.jsp?data=from_get">
<input type="text" name="data" value="from_post">
<input type="submit" value="ok">
</form>
</body>
</html>
the output of this code is
这段代码的输出是
data:from_get
data:from_post
...
Seems like in order to find which parameter came from where, I have to check request.getQueryString
.
似乎为了找到哪个参数来自哪里,我必须检查request.getQueryString
.
回答by Vugluskr
No direct way. Non-direct - check .getQueryString()
没有直接的办法。非直接 - 检查 .getQueryString()
回答by Jason Watts
Why don't you start with checking for query string parameters, if you see none, asume a post and then dig out the form variables?
为什么不从检查查询字符串参数开始,如果没有看到,假设一个帖子然后挖掘表单变量?
回答by Mads Hansen
If you are writing a servlet, you can make that distinction by whether the doGetor doPostmethod is invoked.
如果您正在编写一个 servlet,您可以通过是调用doGet还是doPost方法来区分。
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException{
//Set a variable or invoke the GET specific logic
handleRequest(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException{
//Set a variable or invoke the POST specific logic
handleRequest(request, response);
}
public void handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException{
//Do stuff with the request
}
回答by matt b
HttpServletRequest.getMethod():
HttpServletRequest.getMethod():
Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT. Same as the value of the CGI variable REQUEST_METHOD.
返回发出此请求的 HTTP 方法的名称,例如,GET、POST 或 PUT。与 CGI 变量 REQUEST_METHOD 的值相同。
All you need to do is this:
您需要做的就是:
boolean isPost = "POST".equals(request.getMethod());
Also I'm really confused on why you wouldn't simply use request.getParameter("somename")
to retrieve values sent as request parameters. This method returns the parameter regardless of whether the request was sent via a GET or a POST:
另外我真的很困惑为什么你不会简单地使用request.getParameter("somename")
检索作为请求参数发送的值。无论请求是通过 GET 还是 POST 发送,此方法都会返回参数:
Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.
请求参数是与请求一起发送的额外信息。对于 HTTP servlet,参数包含在查询字符串或发布的表单数据中。
It's a heck of a lot simpler than trying to parse getQueryString()
yourself.
这比尝试解析getQueryString()
自己要简单得多。
回答by Ravi Wallau
I know it doesn't solve your problem, but if I remember correctly, when using Struts, the ActionForm will be populated if your variables are sent in the query string or in the post body.
我知道它不能解决您的问题,但如果我没记错的话,在使用 Struts 时,如果您的变量在查询字符串或帖子正文中发送,则将填充 ActionForm。
I found the source code for the RequestUtils class, maybe the method "populate" is what you need:
我找到了 RequestUtils 类的源代码,也许“populate”方法就是你所需要的:
回答by Sola Lee
I did this inside my Filter so maybe someday someone can use this.
我在我的过滤器中做了这个,所以也许有一天有人可以使用它。
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest sreq = (HttpServletRequest)servletRequest;
System.out.println(sreq.getMethod());
}
回答by itssaifurrehman
You can use request.getMethod() to get any Method. I am using HandlerInterceptor for this.
您可以使用 request.getMethod() 来获取任何方法。我为此使用了 HandlerInterceptor。
check this code:
检查此代码:
public class LoggingMethodInterceptor implements HandlerInterceptor {
Logger log = LoggerFactory.getLogger(LoggingMethodInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
log.info("[START] [" + request.getMethod() + "] [" + request.getRequestURL() + "] StartTime: {} milliseconds",
System.currentTimeMillis());
return true;
}