为什么在java中使用request.getParameter()时字符被破坏?
我在 JSP 页面中有这样一个编码为 big5 的链接 http://hello/world?name=婀ㄉ 当我在浏览器的地址栏中输入它时,它会更改为类似的内容 http://hello/world?name=%23%24%23 当我们想在jsp页面中获取这个参数时,所有的字符都被破坏了。
我们已经这样设置: request.setCharacterEncoding("UTF-8"),这样所有的请求都会转换为UTF8。
但为什么在这种情况下,它不起作用? 提前致谢!。
I have such a link in JSP page with encoding big5
http://hello/world?name=婀ㄉ
And when I input it in browser's URL bar, it will be changed to something like
http://hello/world?name=%23%24%23
And when we want to get this parameter in jsp page, all the characters are corrupted.
And we have set this:
request.setCharacterEncoding("UTF-8"), so all the requests will be converted to UTF8.
But why in this case, it doesn't work ?
Thanks in advance!.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您在浏览器地址栏中输入URL时,浏览器可能会在URL编码之前先转换字符编码。但是,这种行为没有明确定义,请参阅我的问题,
处理 URI 中的字符编码在 Tomcat 上
我们主要在较新的浏览器上获得 UTF-8 和 Latin-1,但在旧浏览器中我们获得各种编码(包括 Big5)。因此,最好避免用户直接输入的 URL 中包含非 ASCII 字符。
如果 URL 嵌入在 JSP 中,您可以通过这样生成它来强制将其转换为 UTF-8,
在 Tomcat 上,需要像这样在 Connector 上指定编码,
您还需要使用
request.setCharacterEncoding("UTF -8")
用于主体编码,但在 servlet 中设置它并不安全,因为这仅在参数未处理但其他过滤器或阀门可能触发处理时才有效。所以你应该在过滤器中进行。 Tomcat 在源代码分发中附带了这样的过滤器。When you enter the URL in browser's address bar, browser may convert the character encoding before URL-encoding. However, this behavior is not well defined, see my question,
Handling Character Encoding in URI on Tomcat
We mostly get UTF-8 and Latin-1 on newer browsers but we get all kinds of encodings (including Big5) in old ones. So it's best to avoid non-ASCII characters in URL entered by user directly.
If the URL is embedded in JSP, you can force it into UTF-8 by generating it like this,
On Tomcat, the encoding needs to be specified on Connector like this,
You also need to use
request.setCharacterEncoding("UTF-8")
for body encoding but it's not safe to set this in servlet because this only works when the parameter is not processed but other filter or valve may trigger the processing. So you should do it in a filter. Tomcat comes with such a filter in the source distribution.为了避免摆弄
server.xml
使用:要在 Tomcat 上实际获取这些参数 你需要做类似的事情:
显然(?)
request.getParameter
URLDecodes() 字符串并将其解释为 < code>iso-8859-1 - 或在server.xml
中设置的URIEncoding
。有关如何从 Tomcat 7 的server.xml
获取URIEncoding
字符集的示例,请参阅 此处To avoid fiddling with the
server.xml
use :To actually get those parameters on Tomcat you need to do something like :
As apparently (?)
request.getParameter
URLDecodes() the string and interprets it asiso-8859-1
- or whatever theURIEncoding
is set to in theserver.xml
. For an example of how to get theURIEncoding
charset from theserver.xml
for Tomcat 7 see hereURL 中不能包含非 ASCII 字符 - 您始终需要对它们进行百分比编码。这样做时,浏览器很难渲染它们。如果您使用 UTF-8 对 URL 进行编码,然后对其进行百分比编码,则渲染效果最佳。对于您的特定 URL,这将给出
http:// hello/world?name=%E5%A9%80%E3%84%89
(检查您的浏览器为此特定链接提供的内容)。当您在 JSP 中获取参数时,需要显式取消引用它,然后从 UTF-8 解码它,因为浏览器将按原样发送它。You cannot have non-ASCII characters in an URL - you always need to percent-encode them. When doing so, browsers have difficulties rendering them. Rendering works best if you encode the URL in UTF-8, and then percent-encode it. For your specific URL, this would give
http://hello/world?name=%E5%A9%80%E3%84%89
(check your browser what it gives for this specific link). When you get the parameter in JSP, you need to explicitly unquote it, and then decode it from UTF-8, as the browser will send it as-is.我在使用 JBoss 7.0 时遇到了问题,我认为这个过滤器解决方案也适用于 Tomcat:
I had a problem with JBoss 7.0, and I think this filter solution also works with Tomcat:
我在这个问题上做了很多搜索,所以这可能会帮助其他在 tomcat 上遇到同样问题的人。这取自 http://wiki.apache.org/tomcat/FAQ/CharacterEncoding 。
(如何在任何地方使用UTF-8)。
上设置 URIEncoding="UTF-8"。参考文献:HTTP 连接器、AJP 连接器。例如,使用 <%@page contentType="text/html; charset=UTF-8" %>对于通常的 JSP 页面,
对于采用 XML 语法的页面(也称为 JSP 文档)。使用response.setContentType("text/html; charset=UTF-8") 或response.setCharacterEncoding("UTF-8")。
I did quite a bit of searching on this issue so this might help others who are experiencing the same problem on tomcat. This is taken from http://wiki.apache.org/tomcat/FAQ/CharacterEncoding.
(How to use UTF-8 everywhere).
<Connector>
in server.xml. References: HTTP Connector, AJP Connector.For example, use <%@page contentType="text/html; charset=UTF-8" %> for the usual JSP pages and
<jsp:directive.page contentType="text/html; charset=UTF-8" />
for the pages in XML syntax (aka JSP Documents).Use response.setContentType("text/html; charset=UTF-8") or response.setCharacterEncoding("UTF-8").