get请求参数传到后台中文乱码,需要转换才行
后台使用request.setCharacterEncoding(encoding);但不生效
String keywork = request.getParameter("keywork");
System.err.println("keywork=" + keywork);
System.err.println("keywork=" + new String(keywork.getBytes("ISO-8859-1"), "gbk"));
/*
打印:
keywork=????
keywork=中文
*/
原来在tomcat里面request.setCharacterEncoding只对POST方法生效
对于GET方法,tomcat7至之前的默认使用ISO-8859-1,tomcat8以后使用utf-8
可以在 $tomcat_home/server.xml指定默认编码
<Connector connectionTimeout="20000" port="8080"
URIEncoding="gbk"
protocol="HTTP/1.1" redirectPort="8443"/>