美文网首页
JavaWeb--Request&Response1

JavaWeb--Request&Response1

作者: 何以解君愁 | 来源:发表于2022-07-20 14:29 被阅读0次

    Map<String,String[]> getParameterMap():获取所有参数Map集合
    String[] getParameterValues(String name):根据名称获取参数值(数组)
    String getParameter(String name):根据名称获取参数值(单个值)

    解决中文乱码(POST):req.setCharacterEncoding("UTF-8");
    乱码原因:Tomcat进行URL解码,默认字符集ISO-8859-1
    URL编码:将字符串按照编码方式转为二进制;每个字节转为2个16进制数并在前面加%
    URL编码实现:URLEncoder.encode(str,"utf-8");
    URL解码实现:URLDecoder.decode(s,"ISO-8859-1");
    解决中文乱码(GET):
    @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("UTF-8");//设置字符输入流编码
            //获取username
            String username = req.getParameter("username");
            //对乱码数据进行编码转为字节数组
            byte[] bytes = username.getBytes(StandardCharsets.ISO_8859_1);
            //字节数组解码
            username = new String(bytes,StandardCharsets.UTF_8);
            //username = new String(username.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        }
    Tomcat8.0后,已将GET请求乱码问题解决,设置默认解码方式为UTF-8
    

    相关文章

      网友评论

          本文标题:JavaWeb--Request&Response1

          本文链接:https://www.haomeiwen.com/subject/anryirtx.html