您的位置:首页 > Web前端

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

2017-07-23 11:01 405 查看
只需要在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");


这样处理之后就会发现在控制台就能正常打印出汉字,并且不乱码了!(学习过程中的一点小问题解决!!!)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: