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

来源:互联网 发布:软件alias怎么读 编辑:程序博客网 时间:2024/05/23 19:13

只需要在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");


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