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);
    }

    }

  • 相关阅读:
    HSV 量化
    小议:部署SharePoint 2013时,无法连接SQL Server问题
    BZOJ 1492 货币兑换 cdq分治或平衡树维护凸包
    初识ASP.NET---一般处理程序
    Vue.js
    jQuery内部原理和实现方式浅析
    关于js中namespace命名空间模式
    MutationObserver 监听DOM树变化
    TCP/UDP常见端口参考
    运算符优先级
  • 原文地址:https://www.cnblogs.com/yuxiaona/p/5964688.html
Copyright © 2011-2022 走看看