用于作为URL的param的Java Servlet getParameter [英] Java Servlet getParameter for a param that is a URL
问题描述
我正在构建一个站点,该站点将一个URL提交给servlet以进行分析。在客户端,我提交url作为编码的参数。例如......
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...
Submit: http://www.site.com
Goes to: http://localhost/myservlet/?url=http%3A%2F%2Fwww.site.com
关于服务器端,我有我的servlet请求参数如此...
On the server side, I have my servlet request the parameter like so...
String url = request.getParameter("url");
我收到的是一个解码后的字符串: http://www.site.com 。到目前为止一切都那么好 - 大部分时间都按预期运行。
What I receive is a decoded string: http://www.site.com. So far so good -- this works as expected... most of the time.
当url param包含自己的参数时会出现问题...
The problem occurs when a url param contains parameters of its own...
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
在客户端站点上一切都很好,但在我的servlet中,当我得到参数时,我只收到url param的一部分!
Everything is fine on the client site, but in my servlet when I get the parameter I receive only part of the url param!
http://www.site.com?param1=1
It从输入的url param中删除了第二个参数!我肯定在将url param提交到服务器之前对其进行编码...发生了什么?
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?
推荐答案
我不能在Tomcat 6.0.29上重现您的问题。事情还有更多。也许链中的过滤器正在对请求对象做一些事情?
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?
无论如何,这是一个单一JSP的 SSCCE :
Anyway, here's a SSCCE in flavor of a single JSP:
<!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并点击链接。在这里,我看到以下结果:
Copy'n'paste'n'run it and click the link. Right here I see the following result:
点击这里
click here
URL:< a href =http://www.site.com?param1=1¶m2=2 =noreferrer> http://www.site.com?param1=1¶m2=2
使用像这样的简单servlet可以重现这一点,它可以直接由浏览器地址栏调用:
The same is reproducible with a simple servlet like this which is invoked directly by browser address bar:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write(request.getParameter("url"));
}
顺便说一句,Tomcat配置为 URIEncoding = HTTP连接器中的UTF-8
,但即使使用 ISO-8859-1
(这是默认值),行为也是-as这个特殊情况 - 相同。
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.
这篇关于用于作为URL的param的Java Servlet getParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!