public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
testGet(request,response);
}
//GET请求方式的中文乱码问题:get提交过来的数据都是查的ISO-8859-1编码.在这个知识点之前建议大家使用POST方式
public void testGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
//拿到原始的字节序列
byte b[] = username.getBytes("ISO-8859-1");
username = new String(b,"UTF-8");
System.out.println(username);
}
//POST请求方式的中文乱码问题
public void testPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");//告知程序客户端提交过来的数据编码,指的请求正文内容编码.对get方式无效
response.setContentType("text/html;charset=UTF-8");
try {
User user = new User();
BeanUtils.populate(user, request.getParameterMap());
response.getWriter().write(user.getUsername());//有没有乱码
} catch (Exception e) {
e.printStackTrace();
}
}