如何确定参数是否已经“发布"?或“获取"从Java? [英] How to determine if a parameter has been "posted" or "geted" from Java?
问题描述
在ASP中,有request.form
和request.queryString
属性,但是在Java中.似乎我们只有一个集合,可以通过request.getParamaterMap
,getParametersNames
,getParameterValues
等访问.
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.
是否可以通过某种方式来判断哪些值已经过帐,哪些值已在URL中指定?
Is there some way to tell which values have been posted and which ones have been specified in the URL?
PS:
我要实现的目标是制作一个可以处理以下情况的页面
What I'm trying to achieve is to make a page that can handle the following situation
- 读取来自查询字符串的变量(获取)
- 阅读具有特定名称(例如"xml")的单个帖子.
- 如果缺少该帖子,请阅读整个正文(
request.getReader()
).
- 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()
).
我正在使用tomcat 6.
I'm using tomcat 6.
根据我到目前为止看到的内容,如果我发布request.getReader()
,则发布的值不再出现在getParamater
集合中,但是querystring参数仍然存在.
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.
另一方面,如果我发出任何getParameters
方法,则getReader
返回空字符串.
On the other hand, if I issue any of the getParameters
methods, getReader
returns empty string.
好像我也不能吃蛋糕.
所以,我想解决的办法是:
so, I guess the solution is the folowwing:
- 用
getReader
读取正文. - 查看xml帖子是否存在(不利之处,我必须手动解析正文.
- 如果是,请获取http消息正文,并删除"xml =部分.
- 如果不是这样,那就去身体吧.
- 通过
request.getParameter
读取querystring参数
- 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
有更好的主意吗?
- PS: Does anybody knows how to parse the body using the same method
used by
HttpServlet
? - PS: Here is a decoding ASP function. Shall I just rewrite it in Java?
- PS: Also found (don't have a machine to test it right now)
只是澄清一下事情.问题似乎是,在getParameter
中,您可以获取发布的值以及随URL传递的值,请考虑以下示例:
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:
<%@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>
此代码的输出是
data:from_get
data:from_post
...
似乎是为了找到哪个参数来自哪里,我必须检查request.getQueryString
.
Seems like in order to find which parameter came from where, I have to check request.getQueryString
.
推荐答案
返回发出此请求的HTTP方法的名称,例如GET,POST或PUT.与CGI变量REQUEST_METHOD的值相同.
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. 您需要做的就是这个 我也很困惑为什么您不会简单地使用 Also I'm really confused on why you wouldn't simply use 请求参数是与请求一起发送的额外信息.对于HTTP Servlet,参数包含在查询字符串或发布的表单数据中. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data. 这比尝试自己解析 It's a heck of a lot simpler than trying to parse 这篇关于如何确定参数是否已经“发布"?或“获取"从Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
boolean isPost = "POST".equals(request.getMethod());
request.getParameter("somename")
来检索作为请求参数发送的值.此方法返回参数,而不管请求是通过GET还是通过POST发送的: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:
getQueryString()
简单得多.getQueryString()
yourself.