Java HttpServletRequest - setCharacterEncoding 似乎什么都不做
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3278900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpServletRequest - setCharacterEncoding seems to do nothing
提问by Erik Sapir
I am trying to read UTF-8 info from the request. I used "request.setCharacterEncoding("UTF-8");", but it seems to do nothing - the info read is non UTF-8.
我正在尝试从请求中读取 UTF-8 信息。我使用了“request.setCharacterEncoding("UTF-8");”,但它似乎什么也没做——读取的信息不是 UTF-8。
What am i doing wrong?
我究竟做错了什么?
采纳答案by Maurice Perry
If you are using tomcat, you should also set the URIEncoding to UTF-8 in your connectors:
如果您使用的是 tomcat,您还应该在连接器中将 URIEncoding 设置为 UTF-8:
<Server port="8105" shutdown="SHUTDOWN">
...
<Service name="Catalina">
<Connector port="8180" URIEncoding="UTF-8" />
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps" />
</Engine>
</Service>
</Server>
回答by sushil bharwani
are you doing it after any request.getParameter call.
在任何 request.getParameter 调用之后,您是否在执行此操作。
request.setCharacterEncoding("UTF-8")
must be called prior to any request.getParameter()
call.
request.setCharacterEncoding("UTF-8")
必须在任何request.getParameter()
调用之前调用。
回答by Virasak
Just to comfirm that for POST parameters you have to call request.setCharacterEncoding(...)
before get parameters.
And for GET parameters, it is depended on what web container you are using (use Maurice Perry's answer for Tomcat).
只是为了确认您必须request.setCharacterEncoding(...)
在获取参数之前调用 POST参数。对于 GET 参数,它取决于您使用的 Web 容器(使用 Maurice Perry 对 Tomcat 的回答)。
Please check this link for more info. "Character Conversions from Browser to Database" http://java.sun.com/developer/technicalArticles/Intl/HTTPCharset/
请查看此链接以获取更多信息。“从浏览器到数据库的字符转换” http://java.sun.com/developer/technicalArticles/Intl/HTTPCharset/
回答by BalusC
The HttpServletRequest#setCharacterEncoding()
has only effect when the request is a POST
request andthe request body is notprocessed yet.
在HttpServletRequest#setCharacterEncoding()
当该请求是一个仅具有效果POST
请求和请求正文是不尚未处理。
So if it doesn't work in your case, then it can have two causes:
因此,如果它在您的情况下不起作用,则可能有两个原因:
You're actually firing a
GET
request. I.e. the request parameters are sent from client to server in the request URL instead of the request body. The request URL is processed by the webserver, not by the Servlet API. So, to fix this, you need to configure the webserver in question to decode the request URL (URI) using the specified character encoding. In case of for example Apache Tomcat, you need to set theURIEncoding
attribute of the<Connector>
element inserver.xml
toUTF-8
.You're correctly using
POST
, but you've already (indirectly) processed the request body so that it's too late to change the character encoding. The request body will be fully processed only whenever the first call on agetParameterXXX()
method is made. There are severalof them. It won't be re-processed on subsequent calls. When nailing down who's calling this method, don't forget to take all declaredFilter
instances inweb.xml
into account. Some of them might grab and scan the parameters.
您实际上是在
GET
发出请求。即请求参数在请求 URL 中而不是请求正文中从客户端发送到服务器。请求 URL 由网络服务器处理,而不是由 Servlet API 处理。因此,要解决此问题,您需要配置相关网络服务器以使用指定的字符编码对请求 URL (URI) 进行解码。例如,在 Apache Tomcat 的情况下,您需要URIEncoding
将<Connector>
元素的属性设置server.xml
为UTF-8
.您正确使用
POST
,但您已经(间接)处理了请求正文,因此更改字符编码为时已晚。只有在第一次调用getParameterXXX()
方法时才会完全处理请求正文。其中有几个。不会在后续调用中重新处理。在确定谁在调用此方法时,不要忘记考虑所有声明的Filter
实例web.xml
。其中一些可能会抓取并扫描参数。
If that still doesn't help anything, then the only possible cause left is that the display console or logger or whatever you're using to print/determine/debug the obtained request parameter does not support UTF-8. You'd like to reconfigure the console/logger/etc to use UTF-8 instead to display the characters. If it's for example the Eclipse console, then you can set it by Window > Preferences > General > Workspace > Text File Encoding.
如果这仍然没有任何帮助,那么唯一可能的原因是显示控制台或记录器或您用来打印/确定/调试获取的请求参数的任何内容不支持 UTF-8。您想重新配置控制台/记录器/等以使用 UTF-8 来显示字符。例如,如果它是 Eclipse 控制台,那么您可以通过Window > Preferences > General > Workspace > Text File Encoding 进行设置。
See also:
也可以看看:
- Unicode - How to get characters right?More background info, practical examples and solutions.
- Unicode - 如何正确获取字符?更多背景信息、实际示例和解决方案。
回答by irreputable
this method is really stupid. it shouldn't be there, and you shouldn't use it.
这个方法真的很蠢。它不应该在那里,你不应该使用它。
for a body in a POST request, the encoding should have been explicitly defined by the client in the Content-Type header. if not, it's a bad request. [1]
对于 POST 请求中的正文,编码应该已由客户端在 Content-Type 标头中明确定义。如果没有,这是一个糟糕的要求。[1]
for a GET request URI, the client cannot specify encoding, and the server must have an implicit encoding, and the programmer needs to set the encoding, yet that method does not exist in Servlet API!
对于GET请求URI,客户端不能指定编码,服务端必须有隐式编码,程序员需要设置编码,而Servlet API中不存在该方法!
however, you servlet container could have a proprietary way of doing that.
但是,您的 servlet 容器可以有一种专有的方式来做到这一点。
the best way is probably set the default encoding of your JVM to UTF-8.
最好的方法可能是将 JVM 的默认编码设置为 UTF-8。
1: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1
1:http: //www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1
The "charset" parameter is used with some media types to define the character set (section 3.4) of the data. When no explicit charset parameter is provided by the sender, media subtypes of the "text" type are defined to have a defaultcharset value of "ISO-8859-1" when received via HTTP. Data in character sets other than "ISO-8859-1" or its subsets MUSTbe labeled with an appropriate charset value.
“charset”参数与一些媒体类型一起使用来定义数据的字符集(第 3.4 节)。当发送方没有提供明确的字符集参数时,“文本”类型的媒体子类型被定义为在通过 HTTP 接收时具有“ISO-8859-1”的默认字符集值。除“ISO-8859-1”或其子集以外的字符集中的数据必须标有适当的字符集值。
回答by ozma
(as for the very first question..)
if you read parameters from the body it is also possible to read each item with its own encoding (look in the last line):
(至于第一个问题..)
如果您从主体中读取参数,也可以使用自己的编码读取每个项目(查看最后一行):
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException ex) {
logger.warn("Fail during file upload");
return uploads;
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
String name = item.getFieldName();
System.out.println("name: " + name);
String value = item.getString();
System.out.println("get as utf8 - "+item.getString("UTF-8"));
回答by hariprasad
The problem is dependent on which application server is used. The best description, which I found in this link.
问题取决于使用的是哪个应用程序服务器。我在此链接中找到的最佳描述。
In some application servers the request.setCharacterEncoding(...)
has no effect until you set the application encoding using a descriptor. The most complicated are JBoss, Apache Tomcat, Glassfish. Better is WebLogic, the best is Jetty (UTF-8 is default setting).
在某些应用程序服务器中,request.setCharacterEncoding(...)
除非您使用描述符设置应用程序编码,否则它无效。最复杂的是JBoss、Apache Tomcat、Glassfish。更好的是WebLogic,最好的是Jetty(UTF-8是默认设置)。
In my case I must create a glassfish-web.xml
descriptor and put there the parameter-encoding
tag. In my case, for GlassFish:
在我的情况下,我必须创建一个glassfish-web.xml
描述符并将parameter-encoding
标签放在那里。就我而言,对于 GlassFish:
<glassfish-web-app error-url="">
<!-- request.setCharacterEncoding("UTF-8") not functioning without this setting-->
<parameter-encoding default-charset="UTF-8" />
</glassfish-web-app>
回答by user1050755
for jboss/wildfly there is a feature request https://issues.jboss.org/browse/WFLY-2533
jboss/wildfly 有一个功能请求https://issues.jboss.org/browse/WFLY-2533
Drop this into WEB-INF/jboss-web.xml:
把它放到 WEB-INF/jboss-web.xml 中:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web version="8.0" xmlns="http://www.jboss.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.org/j2ee/schema/jboss-web_8_0.xsd">
<!-- browser tend to not send encoding information, so we have to match the servlet container's
default encoding with our requested form data encoding: -->
<default-encoding>UTF-8</default-encoding>
</jboss-web>