带有外来字符的 url 的 response.sendredirect - 如何编码?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您如何制作
url
? URI 中不能直接包含非 ASCII 字符;它们必须转换为字节(使用特定的编码),然后进行%编码。应该给
URLEncoder.encode
一个编码参数,以确保这是正确的编码。否则你会得到默认的编码,这可能是错误的并且始终要避免。此 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.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.)
尝试
Try