字符集过滤器导致解析UTF-8字符时出现问题 [英] Charset filter causing issue in parsing UTF-8 characters

查看:283
本文介绍了字符集过滤器导致解析UTF-8字符时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring MVC的字符集过滤器。这是我用来从我的applet调用我的servlet的URL

I am using Spring MVC's charset filter. This is the URL that I use to invoke my servlet from my applet


http://192.168.0.67/MyServlet?p1 =団

你可以看到,参数有一个unicode字符団。所以我使用

As you can see, the parameter has a unicode character 団. So I use

URLEncoder.encode("団", "UTF-8"); 

现在我的网址变成了


http://192.168.0.67/MyServlet?p1=%E5 %9B%A3

但是,从servlet中调用

However, from the servlet, calling

request.getParameter("p1"); 

已经返回一些无法解码的乱码 URLDecoder 。 BTW,invoked

already return some gibberish that cannot be decoded with URLDecoder. BTW, invoking

URLDecoder.decode("%E5%9B%A3", "UTF-8"); 

会提供原始的unicode字元。这只是在servlet甚至可以被解码之前,它的参数已经乱码。有人知道为什么吗? request.getParameter()不使用UTF-8解码参数?

does give me the original unicode character. It's just that the servlet has garbled the parameter before it can even be decoded. Does anyone know why? request.getParameter() doesn't decode parameter with UTF-8?

推荐答案

Spring MVC的字符集过滤器将只设置请求体编码,而不是请求URI编码。您需要在servletcontainer配置中为URI编码设置字符集。很多servlet容器默认ISO-8859-1来解码URI。目前还不清楚您使用的是什么servletcontainer,因此这里只是一个Tomcat的示例:编辑 <$ c /conf/server.xml 的<$ c>< Connector> 条目添加 URIEncoding = UTF-8

The Spring MVC's charset filter will only set the request body encoding, not the request URI encoding. You need to set the charset for the URI encoding in the servletcontainer configuration. Lot of servletcontainers default to ISO-8859-1 to decode the URI. It's unclear what servletcontainer you're using, so here's just an example for Tomcat: edit the <Connector> entry of /conf/server.xml to add URIEncoding="UTF-8":

<Connector ... URIEncoding="UTF-8">

如果由于某种原因(例如第三方托管等)无法编辑服务器的配置,那么你应该考虑使用POST而不是GET:

If you can't edit the server's configuration for some reason (e.g. 3rd party hosting and such), then you should consider to use POST instead of GET:

String query = "p1=" + URLEncoder.encode("団", "UTF-8");
URLConnection connection = new URL(getCodeBase(), "MyServlet").openConnection();
connection.setDoOutput(true); // This sets request method to POST.
connection.getOutputStream().write(query.getBytes("UTF-8"));
// ...

这样你可以在 doPost ()使用 ServletRequest#setCharacterEncoding() 告诉Servlet API使用什么字符集来解析请求体(或者只是依靠Spring MVC的字符集过滤器来完成此任务):

This way you can in doPost() use ServletRequest#setCharacterEncoding() to tell the Servlet API what charset to use to parse the request body (or just rely on the Spring MVC's charset filter from doing this job):

request.setCharacterEncoding("UTF-8");
String p1 = request.getParameter("p1"); // You don't need to decode yourself!
// ...



另请参阅:




  • Unicode - 如何以获得正确的字符?

  • See also:

    • Unicode - How to get the characters right?
    • 这篇关于字符集过滤器导致解析UTF-8字符时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆