关于web前端发送的包含汉字的信息在后台servlet中乱码问题解决

只需要在dopost或doget方法中添加如下语句即可

request.setCharacterEncoding("UTF-8");
如:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
    String user= request.getParameter("username");
    System.out.println(user);
}

其中下面这一句是为了使从服务器输出到客户端的汉字不至于乱码

response.setContentType("text/html;charset=UTF-8");


这样处理之后就会发现在控制台就能正常打印出汉字,并且不乱码了!(学习过程中的一点小问题解决!!!)

猜你喜欢

转载自blog.csdn.net/luoshen87/article/details/75887007
今日推荐