jsp解决中文乱码

1、表单post方式提交,中文乱码处理

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");
2、表单get方式提交,中文乱码处理-治标
String username = request.getParameter("username");

//先将字符串打散成字节数组

byte[] usernames = username.getbytes("ISO-8859-1");

//将字节数组转化为理想格式的字符串

username = new String(username,"utf-8");

    合并一下

username = new String(username.getbytes("ISO-8859-1"),"utf-8");

治本

    *配置Tomcat\conf\servel.xml 文件

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



3、网页后缀

String info = “成功”;
info = URLEncoder.encode(info,"utf-8");
//重定向  response.sendRedirect(request.getContextPath()+"**.jsp?info"+info);    拿到之后如果有乱码还能进行
String info = request.getParameter("info");

info = URLDecoder.encode(info,"utf-8");



 
 




出现中文乱码的问题千奇百怪,需要多自己总结!









猜你喜欢

转载自blog.csdn.net/qq_41902618/article/details/81059843