protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//当时post提交的时候 要注意修改request的码表 必须和浏览器的一致
request.setCharacterEncoding("UTF-8");
String userName= request.getParameter("username");
System.out.println(userName);
//当是 get提交的时候 要注意这个时候修改 request.setCharacterEncoding("UTF-8"); 是没用的 下面是正解
// request 是根据自己的 iso 8859 查询的
byte[] byt=userName.getBytes("iso8859-1"); //将 userName根据 根据 ISO-8859进行反查,查出对应的 乱码字符
userName= new String(byt,"UTF-8");
System.out.println(userName);
}
具体看 http://www.jb51.net/article/56701.htm 这篇
但是在我的实际操作中 我发现当我form用get方式提交的时候,设置
byte source [] = username.getBytes("iso8859-1");
username = new String (source,"UTF-8");
这两句反而会乱码。
后来查看 servlet.xml
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
也没有设置 useBodyEncodingForURI="true" 或者 URLEncoding="ture"
推测 : 我的tomcat 是8.5的版本。可能里面(request)默认查的码表是 utf-8;