zoukankan      html  css  js  c++  java
  • 2016-10-15 给聂玉解决的乱码问题

    //以post方式提交表单
    public class RequestDemo extends HttpServlet {

        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
             //请求数据的中文乱码问题
            request.setCharacterEncoding("UTF-8");//客户端网页我们控制为UTF-8
            String username = request.getParameter("username");
            //获取数据正常,输出数据时可以查阅不同码表
            response.setCharacterEncoding("gb2312");//通知服务器发送数据时查阅的码表
            response.setContentType("text/html;charset=gb2312");//通知浏览器以何种码表打开
            PrintWriter out = response.getWriter();
            out.write(username);

        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request,response);
    }

    //以get方式提交表单
    public class RequestDemo extends HttpServlet {

       public void doGet(HttpServletRequest request, HttpServletResponse response)
               throws ServletException, IOException {
            //请求数据的中文乱码问题
           request.setCharacterEncoding("UTF-8");//以get方式提交数据时,request设置编码无效。即使设置了UTF-8还是会去查ISO8859-1
           String username = request.getParameter("username");
          System.out.println(username);
           byte source [] = username.getBytes("iso8859-1");                         //就是用这两句代码解决的
           username = new String (source,"UTF-8");
           System.out.println(username);
           

       public void doPost(HttpServletRequest request, HttpServletResponse response)
               throws ServletException, IOException {
           doGet(request,response);
    }

    }

  • 相关阅读:
    CF1051F The Shortest Statement 题解
    CF819B Mister B and PR Shifts 题解
    HDU3686 Traffic Real Time Query System 题解
    HDU 5969 最大的位或 题解
    P3295 萌萌哒 题解
    BZOJ1854 连续攻击游戏 题解
    使用Python编写的对拍程序
    CF796C Bank Hacking 题解
    BZOJ2200 道路与航线 题解
    USACO07NOV Cow Relays G 题解
  • 原文地址:https://www.cnblogs.com/yuxiaona/p/5964688.html
Copyright © 2011-2022 走看看