解决方法1:

若使用tomcat, 打开server.xml, 找到如下, 加下URIEncoding="UTF-8", 大概在50行左右

这种方法比较简单, 但是依赖于web容器, 不知道化成其他容器后会不会还有类似的设置?

    <Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" URIEncoding="UTF-8" />

解决方法2:

通过指定编码构造一个新的字符串, 但这种方法用于post会出现问题,问题在于getBytes中的字符编码

post默认不是用“ISO-8859-1”的所以以下方法是用在post传值中是不可取的

condition = new String(condition.getBytes("ISO-8859-1"), "utf-8");

解决方法3:(网上的思路是这样的, 但我试了下不行?)

添加filter, 对参数进行处理

public class CharFilter implements Filter{
	public void destroy() {
	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		chain.doFilter(request, response);
	}
	public void init(FilterConfig filterConfig) throws ServletException {
	}
}



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐