zoukankan      html  css  js  c++  java
  • post请求的中文乱码问题

    post请求的中文乱码问题

    设置请求体的字符集为UTF-8,从而解决post请求的中文乱码问题
    req.setCharacterEncoding("UTF-8");

    这段语句必须在获取请求参数之前调用才有效,如果你先获取了密码的参数,再设置字符集,然后再获取用户名的参数,那么用户名的参数也会乱码的。

    package LWB;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.Arrays;
    
    public class ParameterServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("----------------doGet---------------");
            String username=req.getParameter("username");
            String password=req.getParameter("password");
            String[] hobby=req.getParameterValues("hobby");
            System.out.println("用户名:"+username);
            System.out.println("密码:"+password);
            System.out.println("兴趣爱好:"+ Arrays.asList(hobby));
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //      设置请求体的字符集为UTF-8,从而解决post请求的中文乱码问题
            req.setCharacterEncoding("UTF-8");
            System.out.println("----------------doPost---------------");
            String username=req.getParameter("username");
            String password=req.getParameter("password");
            String[] hobby=req.getParameterValues("hobby");
            System.out.println("用户名:"+username);
            System.out.println("密码:"+password);
            System.out.println("兴趣爱好:"+ Arrays.asList(hobby));
        }
    }
    
    
  • 相关阅读:
    Ajax
    对于本地上关联了远程的git上的项目,删除现有的关联,然后关联新创建的git项目
    UNI-APP_uni-simple-router的快速上手(路由,nui路由拦截)
    js之观察者模式和发布订阅模式区别
    Vue CLI 3.0脚手架如何在本地配置mock数据
    es6解构赋值和扩展运算符
    js Array数组详细操作方法及解析合集
    list排序
    java常用的object数据处理
    java 读写 txt 文件
  • 原文地址:https://www.cnblogs.com/fate-/p/14791696.html
Copyright © 2011-2022 走看看