Java:当参数的值与 java HTTPRequestServlet 中的 UTF-8 编码字符串匹配时,doGet 默认对其进行解码
我是 servlet 新手。我从客户端向 servlet 发出 GET 请求,参数为 param=https%3A%2F%2Fwww.somesite.com。在服务器端,有一个 doGet 方法,它接受 HTTPServletRequest 和 HTTPServletResponse 对象。当我尝试检索参数时,它给我 https://www.somesite.com 而不是 https%3A% 2F%2Fwww.somesite.com。
服务器端代码在Websphere 上运行。这是预期的行为还是有任何可能的解释?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是预期的行为。通过 GET 传递的所有参数都必须进行编码,并且您可能需要手动解码它们,因此 servlet 会为您完成这项工作。如果您不需要这个,您可以使用 java.net.URLEncoder.encode(, "UTF-8")
This is expected behaviour. All parameters passed via GET must be encoded and you will likely need to decode them manually anyway, so servlet does this job for you. If you do not need this, you may use
java.net.URLEncoder.encode(<your_string>, "UTF-8")
这是您作为问题主题放置的引用中所例证的正确行为。
URL 限制使用某些字符。只能使用英文字母、数字和一些特殊字符。如果必须发送其他符号,则必须使用 % 表示法对它们进行编码。例如%20表示空格,%3A表示冒号等。编码由客户端完成。为了方便起见,servlet API 自动解码 servlet 参数。
这是您在文档中阅读并在操作中看到的内容。
This is the correct behaviour exmplined in qoutes you put as your question's subject.
URL is restricted for using some characters. You can only use English letters, digits and some special characters. If you have to send other symbols they have to be encoded using the % notation. For example %20 means space, %3A means colon etc. The encoding is done by client. For convenience servlet API decodes servlet parameters automatically.
This is what you read in documentation and see in action.