用于作为 URL 的参数的 Java Servlet getParameter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3756300/
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
Java Servlet getParameter for a param that is a URL
提问by Michael Balint
I am building a site which submits a url to a servlet for analysis purposes. On the client side, I submit the url as a parameter that is encoded. For example...
我正在构建一个站点,该站点将 url 提交给 servlet 以进行分析。在客户端,我将 url 作为编码的参数提交。例如...
Submit: http://www.site.com
Goes to: http://localhost/myservlet/?url=http%3A%2F%2Fwww.site.com
On the server side, I have my servlet request the parameter like so...
在服务器端,我的 servlet 请求这样的参数......
String url = request.getParameter("url");
What I receive is a decoded string: http://www.site.com. So far so good -- this works as expected... most of the time.
我收到的是一个解码后的字符串:http://www.site.com。到目前为止一切顺利——这在大多数情况下都按预期工作。
The problem occurs when a url param contains parameters of its own...
当 url 参数包含它自己的参数时会出现问题...
Submit: http://www.site.com?param1=1¶m2=2
Goes to: http://localhost/myservlet/?url=http%3A%2F%2Fwww.site.com%3Fparam1%3D1%26param2%3D2
Everything is fine on the client site, but in my servlet when I get the parameter I receive only part of the url param!
在客户端站点上一切正常,但是在我的 servlet 中,当我获得参数时,我只收到了 url 参数的一部分!
http://www.site.com?param1=1
It dropped the second param from my input url param! I am definitely encoding the url param before submitting it to the server... what is going on?
它从我的输入 url 参数中删除了第二个参数!在将 url 参数提交到服务器之前,我肯定会对其进行编码……这是怎么回事?
采纳答案by BalusC
I can't reproduce your problem on Tomcat 6.0.29. There's more at matter. Maybe a Filter in the chain which is doing something with the request object?
我无法在 Tomcat 6.0.29 上重现您的问题。还有更多问题。也许链中的过滤器正在对请求对象执行某些操作?
Anyway, here's a SSCCEin flavor of a single JSP:
无论如何,这是一个具有单个 JSP 风格的SSCCE:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<p><a href="?url=http%3A%2F%2Fwww.site.com%3Fparam1%3D1%26param2%3D2">click here</a>
<p>URL: ${param.url}
</body>
</html>
Copy'n'paste'n'run it and click the link. Right here I see the following result:
复制'n'粘贴'n'运行它并单击链接。在这里,我看到以下结果:
click here
点击这里
网址:http: //www.site.com?param1=1& param2=2
The same is reproducible with a simple servlet like this which is invoked directly by browser address bar:
同样的事情也可以用一个简单的 servlet 重现,它是由浏览器地址栏直接调用的:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write(request.getParameter("url"));
}
Tomcat is by the way configured with URIEncoding="UTF-8"
in HTTP connector, but even with ISO-8859-1
(which is the default), the behaviour is -as expected in this particular case- the same.
顺便说一下,Tomcat 是URIEncoding="UTF-8"
在 HTTP 连接器中配置的,但即使使用ISO-8859-1
(这是默认设置),行为也是 - 正如在这种特殊情况下所预期的 - 相同。