请求参数的中文乱码的解决-HttpServletReques应用

简介: 请求参数的中文乱码的解决-HttpServletReques应用

GET/POST方式的区别:

<body>
<form action="/Demo04/RequestParamsServlet" method="GET/POST">
    用户名:<input type="text" name="username"><br>
    密  &nbsp;&nbsp;&nbsp;码:<input type="password" name="password"><br>
    爱好:
    <input type="checkbox" name="hobby" value="sing">唱歌
    <input type="checkbox" name="hobby" value="dance">跳舞
    <input type="checkbox" name="hobby" value="football">足球<br>
    <input type="submit" value="提交">
</form>
public class RequestParamsServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
    //设置request对象的解码方式
    request.setCharacterEncoding("utf-8");
    String name = request.getParameter("username");
    String password = request.getParameter("password");
      System.out.println("用户名:" + name);
      System.out.println("密  码:" + password);
      // 获取参数名为“hobby”的值
      String[] hobbys = request.getParameterValues("hobby");
      System.out.print("爱好:");
      for (int i = 0; i < hobbys.length; i++) {
        System.out.print(hobbys[i] + ",");
      }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
      doGet(request, response);
    }
  }

观察控制台的效果可以发现:POST方式可以解决,而GET方式不能解决

可以得出结论:setCharacterEncoding()方法只对POST提交方式有效。

对于GET方式,我们可以这样:

public class RequestParamsServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
    //设置request对象的解码方式
    request.setCharacterEncoding("utf-8");
    String name = request.getParameter("username");
    name=new String(name.getBytes("iso8859-1"),"utf-8");
    String password = request.getParameter("password");
      System.out.println("用户名:" + name);
      System.out.println("密  码:" + password);
      // 获取参数名为“hobby”的值
      String[] hobbys = request.getParameterValues("hobby");
      System.out.print("爱好:");
      for (int i = 0; i < hobbys.length; i++) {
        System.out.print(hobbys[i] + ",");
      }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
      doGet(request, response);
    }
  }

添加name=new String(name.getBytes(“iso8859-1”),“utf-8”); 就是先用错误码表"iso8859-1"将用户名重新编码,然后使用utf-8进行解码。

当然还可以l了解一下通过配置Tomcat解决GET提交方式中文乱码问题:

在server.xml中的Connector节点下添加一个useBodyEncodingForURI="true"并在程序中调用response.setCharacterEncoding(“GBK”)。

目录
打赏
0
0
0
0
36
分享
相关文章
浏览器缓存读取规则
浏览器缓存读取规则是指浏览器如何存储和检索网页资源以提高加载速度和减少服务器负载。这些规则包括缓存策略、过期时间及验证机制,确保用户获取最新且高效的内容。
2023云栖大会 阿里云CTO周靖人,打造一朵AI时代最开放的云
2023云栖大会上,阿里云宣布了一项重磅计划:阿里云高校计划,助力高校科研与教育加速,让每位中国在校大学生真实受益于普惠算力。目前,清华大学、北京大学、浙江大学、上海交通大学、中国科学技术大学、华南理工大学等高校已首批达成合作。
89580 307
大模型开发:在构建推荐系统时,你会考虑哪些因素?
构建推荐系统涉及关键因素:用户行为数据(理解兴趣)、物品属性(相似性分析)、上下文信息(时间、地点)、冷启动问题(新用户/物品推荐)、可扩展性与性能(高效算法)、多样性(避免单一推荐)、可解释性(增强信任)和评估优化(准确性和用户满意度)。通过综合运用这些因素,打造精准且有效的推荐服务。
274 1
全新ViT Backbone | PLG-ViT 同时具有并行局部和全局自注意力的轻量化视觉Transformer
全新ViT Backbone | PLG-ViT 同时具有并行局部和全局自注意力的轻量化视觉Transformer
476 0
在windows命令行编译Qt程序并纠错
在windows命令行编译Qt程序并纠错
674 0
在windows命令行编译Qt程序并纠错
AI助理
登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问

你好,我是AI助理

可以解答问题、推荐解决方案等