在doGet方法第一行添加:
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
其中response采用:response.setContentType("text/html;charset=utf-8");
就可以解决乱码问题。
但request需要区分get和post。
//解决post请求乱码
request.setCharacterEncoding("utf-8");
//解决get请求乱码
String username=request.getParameter("username");
//手动编解码
byte[] bytes=username.getBytes("iso8859-1");
username=new String(bytes,"utf-8");
System.out.println("用户名"+username);