当前位置:网站首页>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));
}边栏推荐
猜你喜欢
![[E-commerce operation] Do you really understand social media marketing (SMM)?](/img/5b/6682c613305deb3dc15401077d38a0.png)
[E-commerce operation] Do you really understand social media marketing (SMM)?

自媒体爆款标题怎么写?手把手教你写热门标题

Where can I view the version record of WeChat applet submission review history?

再有人问你分布式事务,把这篇扔给他

2016,还是到了最后

建校仅11年就入选“双一流” ,这所高校是凭什么做到的?

零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?

常量及数据类型你还记得多少?

怎么加入自媒体,了解这5种变现模式,让账号快速变现

网络套接字(UDP和TCP编程)
随机推荐
[E-commerce operation] Do you really understand social media marketing (SMM)?
基于UiAutomator2+PageObject模式开展APP自动化测试实战
Configuration swagger
力扣练习——56 寻找右区间
If someone asks you about distributed transactions again, throw this to him
2016,还是到了最后
LAXCUS分布式操作系统安全管理
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
有哪些好用的性能测试工具推荐?性能测试报告收费标准
不止跑路,拯救误操作rm -rf /*的小伙儿
模块九 - 设计电商秒杀系统
Buckle exercise - rectangular area does not exceed the maximum value of K and (hard)
leetcode 823. Binary Trees With Factors(因子二叉树)
It is rumored that Samsung 3nm has won the second customer, and the current production capacity is in short supply
迈矽科推出高性能77GHz毫米波雷达芯片,尚未量产就已获数万颗订单
力扣练习——62 有效的数独
被面试官问到消息队列的丢失、重复与积压问题该如何回答
项目部署、
做自媒体月入几万?博主们都在用的几个自媒体工具
LeetCode 369. Plus One Linked List(链表加1)