java 使用 & 符号从 url 获取请求参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10937652/
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
get the request parameter from url with & symbol
提问by Ars
I am trying to get the request parameter which has '&' sybol in starting like:-
http://localhost:8080/simple.jsp?my=&234587
我正在尝试获取以 '&' sybol 开头的请求参数,例如:-
http://localhost:8080/simple.jsp?my=&234587
On other page I'm getting it like String value=request.getParameter("my");
value.substring(0,4);
在其他页面上我得到它喜欢 String value=request.getParameter("my");
value.substring(0,4);
I want to get &234, please suggest i am not getting any value.
我想得到 &234,请建议我没有得到任何价值。
Thanks, Ars
谢谢,阿尔斯
采纳答案by Mark Rotteveel
In this example you have not one, but two parameters:
在此示例中,您没有一个参数,而是两个参数:
- my
- 234
- 我的
- 234
234
is not a value here. The &
separates query parameters. If you need that ampersand to be part of the value of my
, it needs to be escaped in the URL as %26
.
234
不是这里的值。在&
中隔离查询参数。如果您需要该符号作为 的值的一部分my
,则需要在 URL 中将其转义为%26
。
回答by Ars
thanks for resonse i found the answer, I used request.getQueryString();
to get the whole string i.e. &234587 and then parsed it accordingly.
感谢您的回答,我找到了答案,我曾经request.getQueryString();
得到整个字符串,即 &234587,然后相应地解析它。
:)
:)
Thanks once again.
再次感谢。
回答by kevinpeterson
You will most likely have to encode the '&' in the url:
您很可能必须在 url 中对“&”进行编码:
http://localhost:8080/simple.jsp?my=%26234587
http://localhost:8080/simple.jsp?my=%26234587
回答by Akhi
If you will be handling only Get request. You can write something like this.
如果您将只处理 Get 请求。你可以写这样的东西。
String requestURI=request.getRequestURI();
String pContent=requestURI.split("?")[1];
String[] pArray= pContent.split("&");
String value="";
for (int i=0 ;i<pArray.length;i++) {
if(pArray[i].equals("my"+"=")){
value="&" + pArray[i+1];
break;
}
}