带有外来字符的 url 的 response.sendredirect - 如何编码?

发布于 2024-11-02 14:11:49 字数 454 浏览 1 评论 0原文

我有一个 jsf 应用程序,它有国际用户,因此表单输入可以有非西方字符串,如汉字和中文 - 如果我用 ..?q=东日本大 点击我的网址,页面上的输出是正确的,我看到 q 输入我的表格填充得很好。但是,如果我在表单中输入相同的字符串并提交,我的应用程序会在使用 url 中填充的参数构造 url 后重定向回自身(似乎多余,但这是由于第 3 方集成所致),但重定向未编码正确地连接字符串。我有

url = new String(url.getBytes("ISO-8859-1"), "UTF-8");
response.sendRedirect(url);

但是 url 重定向最终是 q=????我在 String 构造函数中使用了各种编码字符串(在 ISO 和 UTF-8 之间切换,只是在 url 中得到了一堆乱码),但似乎没有一个对我得到 q=东日本大 有任何想法我需要做什么才能正确填充重定向中的 q=东日本大?谢谢。

I have a jsf app that has international users so form inputs can have non-western strings like kanjii and chinese - if I hit my url with ..?q=東日本大 the output on the page is correct and I see the q input in my form gets populated fine. But if I enter that same string into my form and submit, my app does a redirect back to itself after constructing the url with the populated parameters in the url (seems redundant but this is due to 3rd party integration) but the redirect is not encoding the string properly. I have

url = new String(url.getBytes("ISO-8859-1"), "UTF-8");
response.sendRedirect(url);

But url redirect ends up being q=???? I've played around with various encoding strings (switched around ISO and UTF-8 and just got a bunch of gibberish in the url) in the String constructor but none seem to work to where I get q=東日本大 Any ideas as to what I need to do to get the q=東日本大 populated in the redirect properly? Thanks.

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

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

发布评论

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

评论(2

沧桑㈠ 2024-11-09 14:11:49

您如何制作url? URI 中不能直接包含非 ASCII 字符;它们必须转换为字节(使用特定的编码),然后进行%编码。

应该给 URLEncoder.encode 一个编码参数,以确保这是正确的编码。否则你会得到默认的编码,这可能是错误的并且始终要避免。

String q= "\u6771\u65e5\u672c\u5927"; // 東日本大

String url= "http://example.com/query?q="+URLEncoder.encode(q, "utf-8");
// http://example.com/query?q=%E6%9D%B1%E6%97%A5%E6%9C%AC%E5%A4%A7

response.sendRedirect(url);

此 URI 将在浏览器地址栏中显示为 IRI 'http://example.com/query?q=东日本大'。

确保您以 UTF-8 格式提供页面(使用 Content-Type header/meta)并将查询字符串输入解释为 UTF-8(特定于服务器;请参阅 此常见问题解答 适用于 Tomcat。)

How are you making your url? URIs can't directly have non-ASCII characters in; they have to be turned into bytes (using a particular encoding) and then %-encoded.

URLEncoder.encode should be given an encoding argument, to ensure this is the right encoding. Otherwise you get the default encoding, which is probably wrong and always to be avoided.

String q= "\u6771\u65e5\u672c\u5927"; // 東日本大

String url= "http://example.com/query?q="+URLEncoder.encode(q, "utf-8");
// http://example.com/query?q=%E6%9D%B1%E6%97%A5%E6%9C%AC%E5%A4%A7

response.sendRedirect(url);

This URI will display as the IRI ‘http://example.com/query?q=東日本大’ in the browser address bar.

Make sure you're serving your pages as UTF-8 (using Content-Type header/meta) and interpreting query string input as UTF-8 (server-specific; see this faq for Tomcat.)

葬﹪忆之殇 2024-11-09 14:11:49

尝试

response.setContentType("text/html; charset=UTF-16");
response.setCharacterEncoding("utf-16");

Try

response.setContentType("text/html; charset=UTF-16");
response.setCharacterEncoding("utf-16");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文