zoukankan      html  css  js  c++  java
  • request请求编码处理

    request用来获取浏览器的请求信息,请求的方式分为get请求和post请求。

    get请求会把参数显示到地址栏(超链接或通过地址栏直接请求),post请求用来提交表单。

    get请求的参数在url之后,post请求的参数在请求体中

    如果请求的参数含有中文,需要对其请求进行编码进行设置

    post:

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");//处理post请求的编码,只对post请求有效
            String name = request.getParameter("username");
            System.out.println(name);
    
        }

    get:

        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            /**
             * 页面请求的编码是utf-8,request获取请求的参数默认使用iso-8859-1编码,所以会乱码
             */
            String id = request.getParameter("id");
            byte[] bytes = id.getBytes("ISO-8859-1");//把得到的id解码,得到原来的utf-8字节数组
            id = new String(bytes, "UTF-8");//再对这个utf-8数组进行二次编码
            System.out.println(id);
    
        }

    resuest请求编码由响应这个页面的编码决定(上一次请求服务器,响应的是什么编码 ,这回发送过去的就是什么编码)

    ps:getBytes()是将一个字符串转化为一个字节数组。

  • 相关阅读:
    规矩与管理
    信息系统叫设施比叫工具更贴近本义
    让ansbile和docker愉快的在一起
    elasearch基础教程
    markdown语法
    python 实用pickle序列化
    python 解析配置文件
    ansible状态管理
    haproxy官方配置文档地址
    ansible操作模块相关
  • 原文地址:https://www.cnblogs.com/sflik/p/4570466.html
Copyright © 2011-2022 走看看