使用tomcat get请求中文乱码

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"/>
发布了95 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_26264237/article/details/103781725