当我们遇到乱码的问题就想是编码的时候出现问题还是解码的时候出现问题。
<form action="test" method="post">
<input type="text" name="value">
<input type="submit" value="提交">
</form>
//如果不设置编码默认获取到的就是浏览器的默认编码,java设置的是utf-8,解码不一致会出现乱码
req.setCharacterEncoding("utf-8");
String value = req.getParameter("value");
System.out.println(value);
response.setContentType(“text/html;charset=UTF-8”); 目的是为了控制浏览器的行为,即控制浏览器用UTF-8进行解码;
response.setCharacterEncoding(“UTF-8”); 的目的是用于response.getWriter()输出的字符流的乱码问题,如果是response.getOutputStream()是不需要此种解决方案的;因为这句话的意思是为了将response对象中的数据以UTF-8解码后发向浏览器;