表单

get

  1. 获取数据前 分解成字节,然后封装成 UTF-8
    • String user = new String(request.getParameter(“user”).getBytes(“iso-8859-1”), “UTF-8”);
  2. 配置服务器,也可解决:
    1
    2
    3
    <Connector port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" URIEncoding="UTF-8"/>

post

  • 直接在接收 servlet 中设置 request.setCharacterEncoding(“UTF-8”);

超链接

1
<a href="http://www.qq.com?user=你好"></a>

处理办法和 get 是一样的

sendRedirect(), 下载文件名中文乱码的处理:

1
2
3
4
5
response.sendRedirect("主机?username=" + "你好");
String temp = java.net.URLEncoder.encode("你好.mp3","UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + temp);
response.setContentType("text/html;charset=utf-8");//设置相应字符编码,告诉浏览器采用utf-8
request.setCharacterEncoding("UTF-8");//设置请求体的字符编码