zoukankan      html  css  js  c++  java
  • post请求的中文乱码问题

    post请求的中文乱码问题

    设置请求体的字符集为UTF-8,从而解决post请求的中文乱码问题
    req.setCharacterEncoding("UTF-8");

    这段语句必须在获取请求参数之前调用才有效,如果你先获取了密码的参数,再设置字符集,然后再获取用户名的参数,那么用户名的参数也会乱码的。

    package LWB;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.Arrays;
    
    public class ParameterServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("----------------doGet---------------");
            String username=req.getParameter("username");
            String password=req.getParameter("password");
            String[] hobby=req.getParameterValues("hobby");
            System.out.println("用户名:"+username);
            System.out.println("密码:"+password);
            System.out.println("兴趣爱好:"+ Arrays.asList(hobby));
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //      设置请求体的字符集为UTF-8,从而解决post请求的中文乱码问题
            req.setCharacterEncoding("UTF-8");
            System.out.println("----------------doPost---------------");
            String username=req.getParameter("username");
            String password=req.getParameter("password");
            String[] hobby=req.getParameterValues("hobby");
            System.out.println("用户名:"+username);
            System.out.println("密码:"+password);
            System.out.println("兴趣爱好:"+ Arrays.asList(hobby));
        }
    }
    
    
  • 相关阅读:
    ArcObjects SDK(AE)10.1在vs2012安装的方法(亲测,并未成功,尝试需谨慎)
    PTA 求链式表的表长
    PTA 输出数组元素
    PTA 找出不是两个数组共有的元素
    PTA 将数组中的数逆序存放
    PTA 利用指针找最大值
    PTA 有序数组的插入
    PTA 冒泡排序
    PTA 报数
    PTA 数组循环右移
  • 原文地址:https://www.cnblogs.com/fate-/p/14791696.html
Copyright © 2011-2022 走看看