在tomcat的server.xml文件中修下面的代碼片段,在最后添加URIEncoding="UTF-8",修改后的結(jié)果如下
Xml代碼
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8">
二、POST亂碼解決方法
自定義字符集過濾器,代碼如下
Java代碼
package com.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharEncodingFilter implements Filter {
private String charSet;
public void init(FilterConfig config) throws ServletException {
charSet = config.getInitParameter("encoding");
if (charSet == null && charSet.length() < 1) {
charSet = "UTF-8";
}
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filter) throws IOException, ServletException {
request.setCharacterEncoding(this.getCharSet());
response.setCharacterEncoding(this.getCharSet());
filter.doFilter(request, response);
}
public void destroy() {
this.setCharSet(null);
}
public void setCharSet(String charSet) {
this.charSet = charSet;
}
public String getCharSet() {
return (this.charSet);
}
}
web.xml配置
Xml代碼
CharEncodingFilter
com.filter.CharEncodingFilter
encoding
UTF-8