request接收中文字符串乱码:
String old=request.getParameter("name");//假设name字段为中文
String nameUTF_8 = new String(old.getBytes("ISO8859-1"),"UTF-8");
String nameGB2312 = new String(old.getBytes("ISO8859-1"),"gb2312");
String nameGBK = new String(old.getBytes("UTF-8"),"GBK");
String nameEncoded = URLEncoder.encode(old, "UTF-8");
String nameDecoded = URLDecoder.decode(old, "UTF-8");
System.out.println("old: \t" + old);
System.out.println("nameUTF-8: \t" + nameUTF_8);
System.out.println("nameGB2312: \t" + nameGB2312);
System.out.println("nameGBK: \t" + nameGBK);
System.out.println("nameEncoded: \t" + nameEncoded);
System.out.println("nameDecoded: \t" + nameDecoded);
response 输出中文:
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter printWriter = response.getWriter();
printWriter.print( content );