Java 如何在URL中传递多个参数?
我试图弄清楚如何在URL中传递多个参数。我想将纬度和经度从我的android类传递到java servlet。我该怎么做Java 如何在URL中传递多个参数?,java,android,url,servlets,Java,Android,Url,Servlets,我试图弄清楚如何在URL中传递多个参数。我想将纬度和经度从我的android类传递到java servlet。我该怎么做 URL url; double lat=touchedPoint.getLatitudeE6() / 1E6; double lon=touchedPoint.getLongitudeE6() / 1E6; url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+lon); url = n
URL url;
double lat=touchedPoint.getLatitudeE6() / 1E6;
double lon=touchedPoint.getLongitudeE6() / 1E6;
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+lon);
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
在这种情况下,输出(写入文件)为28.53438677.472097
。
这是可行的,但我想在两个单独的参数中传递纬度和经度,以便减少我在服务器端的工作量。如果不可能,我如何至少在lat
和lon
之间添加一个空格,以便使用tokenizer
类获取纬度和经度。我试着跟着台词走,但没用
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
我的servlet代码如下所示:
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
final String par1 = req.getParameter("param1");
final String par2 = req.getParameter("param2");
FileWriter fstream = new FileWriter("C:\\Users\\Hitchhiker\\Desktop\\out2.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(par1);
out.append(par2);
out.close();
我还想知道,这是将数据从android设备传递到服务器的最安全的方式。我对Java知之甚少,但URL查询参数应该用“&”分隔,而不是“?”
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
使用“sub delim”作为关键字是一个很好的参考位置。是另一个很好的来源。这
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"¶m2="+lon);
必须工作。出于任何奇怪的原因1,在第一个参数之前需要?
,在下列参数之前需要&
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
使用复合参数,如
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"_"+lon);
这也行,但肯定不好。您不能在那里使用空格,因为它在URL中是被禁止的,但您可以将其编码为%20
或+
(但这是更糟糕的样式)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
1声明
?
将路径和参数分开,并且和将参数彼此分开,并不解释任何原因。一些RFC说“使用?那里和那里”,但我不明白他们为什么不选择相同的字符。您可以将多个参数作为“?param1=value1¶m2=value2
”传递
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
但这并不安全。它容易受到跨站点脚本(XSS)攻击
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
您的参数可以简单地替换为脚本
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
看看这个,然后
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
您可以使用的API使其安全
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
即使在没有上述预防措施的情况下使用https
url进行安全保护,也不是一个好的做法
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
查看相关SE问题:
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
应该是¶m2
而不是?param2
谢谢,工作起来很有魅力。:)你能回答我最后一个问题吗?这是最安全的方法吗?我正在开发的应用程序将部署在市场上,因此它必须是安全的。关于安全性,答案当然取决于您需要什么样的安全性。如果你想防止未经授权使用你的应用程序,你可能会失去锁定。对于安全通信,HTTPS
通常是一种方式。在客户端,您只需要添加一个“s”,在服务器端,您需要一个私钥和一个由CA签名的证书。或者您可以运行自己的证书,这需要更多的工作。然而。。。这是一个不同的问题,发布它。这不是一个奇怪的原因。?
不是查询字符串的一部分。它只是请求URI和请求查询字符串之间的分隔符。查询字符串参数对依次需要&
作为分隔符。查询字符串参数名称和值对依次需要=
作为分隔符。顺便说一下,查询字符串中的空格应编码为+
。%20
仅适用于请求URI部分。您可以使用urlcoder\encode()
对查询字符串组件进行编码。我也看到了,但是一些东西?a=1?b=2
更容易编写和解析;使用&
而不是?
作为分隔符没有任何逻辑上的原因。AFAIK+
只是``的便捷快捷方式,URLDecoder
也接受%20
。LDAP以各种方式使用这种格式,但开始讨论大约在1992年做出的决定是徒劳的@关于编码要求,BalusC是正确的。您不能依赖某个特定API的基本特性。不是所有的东西都是Java,也不是所有的东西都是由URLDecover解码的。
我在StringEscapeUtils
中没有看到任何正确编码URL参数的东西,并且URLDecover
已经存在。更新了相关SE问题的答案。
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996