Java:当参数的值与 java HTTPRequestServlet 中的 UTF-8 编码字符串匹配时,doGet 默认对其进行解码

发布于 2024-12-05 12:16:20 字数 331 浏览 5 评论 0 原文

我是 servlet 新手。我从客户端向 servlet 发出 GET 请求,参数为 param=https%3A%2F%2Fwww.somesite.com。在服务器端,有一个 doGet 方法,它接受 HTTPServletRequest 和 HTTPServletResponse 对象。当我尝试检索参数时,它给我 https://www.somesite.com 而不是 https%3A% 2F%2Fwww.somesite.com。

服务器端代码在Websphere 上运行。这是预期的行为还是有任何可能的解释?

I am new to servlets. I issue a GET request on a servlet with a parameter say param=https%3A%2F%2Fwww.somesite.com from the client side. On the server side, there is a doGet method that takes HTTPServletRequest and HTTPServletResponse object. When i try to retrieve param, it gives me https://www.somesite.com instead of https%3A%2F%2Fwww.somesite.com.

The server side code runs on Websphere. Is it an expected behavior or is there any possible explanation for the same ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒时光 2024-12-12 12:16:20

这是预期的行为。通过 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")

原来是傀儡 2024-12-12 12:16:20

这是您作为问题主题放置的引用中所例证的正确行为。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文