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

    }

  • 相关阅读:
    table中tr间距的设定table合并单元格 colspan(跨列)和rowspan(跨行)
    使用jquery触发a标签跳转
    真正的让iframe自适应高度 兼容多种浏览器随着窗口大小改变
    html5 data属性的使用
    jQuery取得select选择的文本与值
    jqueryui教程
    密码复杂度
    zabbix配置微信报警
    tomcat配置域名访问
    阿里云ecs禁止ping,禁止telnet
  • 原文地址:https://www.cnblogs.com/yuxiaona/p/5964688.html
Copyright © 2011-2022 走看看