当前位置:网站首页>Servlet---解决post请求中中文乱码问题
Servlet---解决post请求中中文乱码问题
2022-08-10 11:16:00 【我爱布朗熊】
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 解决中文乱码问题
req.setCharacterEncoding("UTF-8");
// 获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
String hobby = req.getParameter("hobby");
// 有多个值的话要使用这个方法
String[] hobby2 = req.getParameterValues("hobby");
System.out.println("username=>"+username);
System.out.println("password=>"+password);
System.out.println("hobby=>"+hobby);
System.out.println("hobby=>"+ Arrays.toString(hobby2));
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8080/web_Tomcat_war_exploded/ParameterServlet" method="post">
用户名: <input type="text" name="username"/> <br/>
密码: <input type="password" name="password"/> <br/>
兴趣爱好: <input type="checkbox" name="hobby" value="java"/> Java
<input type="checkbox" name="hobby" value="c++"/>C++
<input type="checkbox" name="hobby" value="js"/> JavaScript
<br/>
<input type="submit">
</form>
</body>
</html>当我们的请求是post请求的时候,如果表单中有中文的话,会出现中文乱码的问题
当然解决这个问题的方式很简单,就是修改一下字符编码集
req.setCharacterEncoding("UTF-8");
注意!!!!!!
req.setCharacterEncoding("UTF-8"); 此语句一定要在获取请求的参数之前使用才可以实现效果
像下面的代码一样,还是会出现乱码问题!!!!
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
// 解决中文乱码问题
req.setCharacterEncoding("UTF-8");
// 获取请求参数
String password = req.getParameter("password");
// 有多个值的话要使用这个方法
String[] hobby = req.getParameterValues("hobby");
System.out.println("username=>"+username);
System.out.println("password=>"+password);
System.out.println("hobby=>"+ Arrays.toString(hobby));
}边栏推荐
- LeetCode 362. Design Hit Counter
- Samsung plans to start producing semiconductor components in Vietnam in 2023
- OPNsense安装配置Zenarmor
- 力扣练习——62 有效的数独
- Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
- 建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
- 中芯CIM国产化项目暂停?上扬软件:未停摆,改为远程开发!
- LeetCode 146. LRU 缓存
- Analysis of the implementation principle of UUID from the perspective of source code
- APP automation testing practice based on UiAutomator2+PageObject mode
猜你喜欢

Does your child lack self-discipline?Ape Counseling: Pay attention to "blank" in the schedule to give children more control

网络基础(第一节)

A case of violent parameter tuning in machine learning

即时零售业态下如何实现自动做账?

Since the media hot style title how to write?Taught you how to write the title

个推数据资产管理经验 | 教你打造数据质量心电图,智能检测数据“心跳”异常

rider内Mono脚本找不到引用资源

推荐6个自媒体领域,轻松易上手

Network sockets (UDP and TCP programming)

StoneDB 文档捉虫活动第一季
随机推荐
LeetCode 21. 合并两个有序链表
LeetCode 83. Remove Duplicate Elements in Sorted List
单目操作符(含原码反码补码转换)
孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感
Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability
Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using
LeetCode 24. Swap nodes in linked list pairwise
LeetCode 83. 删除排序链表中的重复元素
制品库是什么?
被面试官问到消息队列的丢失、重复与积压问题该如何回答
LeetCode 445. Adding Two Numbers II
网络套接字(UDP和TCP编程)
Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
LeetCode 109. 有序链表转换二叉搜索树
What are some useful performance testing tools recommended? Performance testing report charging standards
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
Introduction to Software Architecture
A case of violent parameter tuning in machine learning