中文乱码处理
发生中文乱码有三种情况
①表单form
(1)post
解决方法:这接受方设置适当的编码
request.setCharacterEncoding("utf-8");
(2)get
可以写一个工具类
public class MyTools{
public static String getNewString (String str){
new str=(str.getBytes(),"utf-8");
}
return str;
}
②超链接
<a href="http://www.sohu.com?name=sun">
点击</a>
超链接本质是get提交,可以用get的解决方案
③sendRedirect()发生乱码
response.sendRedirect("地址?name=sun");
说明:我们应当尽量使用post提交
在服务器是中文,在response的时候,也要考虑浏览器显示是否正确,一般我们通过
response.setContentType("text/html;charset=utf-8");
发生中文乱码有三种情况
①表单form
(1)post
解决方法:这接受方设置适当的编码
request.setCharacterEncoding("utf-8");
(2)get
可以写一个工具类
public class MyTools{
public static String getNewString (String str){
new str=(str.getBytes(),"utf-8");
}
return str;
}
②超链接
<a href="http://www.sohu.com?name=sun">
点击</a>
超链接本质是get提交,可以用get的解决方案
③sendRedirect()发生乱码
response.sendRedirect("地址?name=sun");
说明:我们应当尽量使用post提交
在服务器是中文,在response的时候,也要考虑浏览器显示是否正确,一般我们通过
response.setContentType("text/html;charset=utf-8");
这个跟sendRedirect()的特殊性有关,相当于再次提交,在接收的时候还需要在转.
④当我们下载文件的时候,可能提示是中文乱码,可以这样解决
String temp=java.net.URLEncoder.encode("传奇.mp3","utf-8");
response.setHeader("Content-Disposition","attachment;filename="+temp);