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();
}
}解决中文请求参数乱码问题
最新推荐文章于 2023-01-04 23:53:18 发布
本文详细介绍了如何处理HTTP GET和POST请求中出现的中文乱码问题,并提供了具体的实现代码示例。针对GET请求,文章讲解了如何通过重新设置字符集来避免乱码;对于POST请求,则展示了如何配置请求和响应的编码。
6259

被折叠的 条评论
为什么被折叠?



