[英]Jetty response character encoding
How do I set the default character encoding on my responses to UTF-8? 如何在对UTF-8的响应中设置默认字符编码?
I've tried this 我试过这个
System.setProperty("file.encoding", "UTF-8");
and this 还有这个
System.setProperty("org.eclipse.jetty.util.UrlEncoding.charset", "utf-8");
Neither has any effect - responses are still sent with the header 两者都没有任何效果 - 响应仍然与标题一起发送
Content-Type: text/html; charset=ISO-8859-1
I'd like to do this for all text/html responses, and ideally in code rather than XML. 我想为所有text / html响应做这个,理想情况是代码而不是XML。 I'm using Jetty 9.
我正在使用Jetty 9。
The Jetty documentation claims it uses UTF-8 by default, but that seems to be a lie. Jetty文档声称它默认使用UTF-8,但这似乎是一个谎言。 If you do the normal
response.getWrite().println("Hello")
, then the content encoding is determined as follows. 如果执行正常的
response.getWrite().println("Hello")
,则内容编码确定如下。
org/eclipse/jetty/http/encoding.properties
: org/eclipse/jetty/http/encoding.properties
加载从content-type到content-encoding的默认映射: // MimeTypes.java:155
ResourceBundle encoding = ResourceBundle.getBundle("org/eclipse/jetty/http/encoding");
Enumeration<String> i = encoding.getKeys();
while(i.hasMoreElements())
{
String type = i.nextElement();
__encodings.put(type,encoding.getString(type));
}
The default file is: 默认文件是:
text/html = ISO-8859-1
text/plain = ISO-8859-1
text/xml = UTF-8
text/json = UTF-8
Response.getWriter()
tries to use that map, but defaults to ISO-8859-1 Response.getWriter()
尝试使用该映射,但默认为ISO-8859-1 @Override
public PrintWriter getWriter() throws IOException
{
if (_outputType == OutputType.STREAM)
throw new IllegalStateException("STREAM");
if (_outputType == OutputType.NONE)
{
/* get encoding from Content-Type header */
String encoding = _characterEncoding;
if (encoding == null)
{
encoding = MimeTypes.inferCharsetFromContentType(_contentType);
if (encoding == null)
encoding = StringUtil.__ISO_8859_1;
setCharacterEncoding(encoding);
}
So you can see that for text/html
it doesn't default to UTF-8. 所以你可以看到,对于
text/html
它不会默认为UTF-8。 I don't think there is a way of changing the default from code. 我认为没有办法从代码中更改默认值。 The best you can do is change the
encoding.properties
file to this: 您可以做的最好是将
encoding.properties
文件更改为:
text/html = UTF-8
text/plain = UTF-8
text/xml = UTF-8
text/json = UTF-8
But even then if it finds an encoding that isn't in there it will default to ISO-8859-1. 但即便如此,如果找到不在其中的编码,它将默认为ISO-8859-1。
response.setCharacterEncoding("UTF-8");
It matter when you use Writer(); 使用Writer()时很重要;
For me If I write 对我来说,如果我写
resp.getWriter().println("Return");
resp.setContentType("text/html; charset=UTF-8");
I won't work 我不会工作
But if I change the sequence 但如果我改变序列
resp.setContentType("text/html; charset=UTF-8");
resp.getWriter().println("Return");
It will be alright 它会好起来的
I created character encoding filter to one legacy application. 我为一个遗留应用程序创建了字符编码过滤器
public class CharacterEncodingFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
if(req instanceof Request){
req.setCharacterEncoding("UTF-8");
}
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
@Override
public void destroy() {
}
}
In web.xml filter-mapping has the url-pattern of /*. 在web.xml中,filter-mapping具有/ *的url-pattern。 This routes all requests from the web application through the CharacterEncodingFilter.
这将通过CharacterEncodingFilter路由来自Web应用程序的所有请求。
<filter>
<display-name>CharacterEncoding</display-name>
<filter-name>CharacterEncoding</filter-name>
<filter-class>my.app.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
You can change the default UTF-8
charset to ISO-8859-1
for example. 例如,您可以将默认的
UTF-8
字符集更改为ISO-8859-1
。 The documentation does not make it very clear which parameter name for versions later than 9.3. 该文档没有明确说明9.3之后版本的参数名称。 Before 9.3 it was
org.eclipse.jetty.util.URI.charset
For new versions it has been changed to org.eclipse.jetty.util.UrlEncoding.charset
Here's an example: 在9.3之前它是
org.eclipse.jetty.util.URI.charset
对于新版本,它已被更改为org.eclipse.jetty.util.UrlEncoding.charset
这是一个例子:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.15.v20190215</version>
<configuration>
<systemPropertiesFile>src/main/config/jetty/encode.properties</systemPropertiesFile>
<jettyXml>src/main/config/jetty/jetty-env.xml</jettyXml>
</configuration>
</plugin>
content for encode.properties encode.properties的内容
org.eclipse.jetty.util.UrlEncoding.charset=ISO-8859-1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.