当前位置:网站首页>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));
}
边栏推荐
猜你喜欢
再有人问你分布式事务,把这篇扔给他
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
单目操作符(含原码反码补码转换)
Network sockets (UDP and TCP programming)
学长告诉我,大厂MySQL都是通过SSH连接的
std::move()
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
Module 9 - Designing an e-commerce seckill system
Nocalhost - Making development more efficient in the cloud-native era
mpf6_Time Series Data_quandl_correct kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
随机推荐
实现内网穿透的最佳解决方案(无实名认证,完全免费)
面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
WeChat applet, global variables change in one place and the state in other places also changes.
The author of open source also has a life problem
力扣练习——62 有效的数独
彩色图和深度图转点云
How many constants and data types do you remember?
ENVI 5.3软件安装包和安装教程
LeetCode 61. 旋转链表
2016,还是到了最后
可视化服务编排在金融APP中的实践
Nocalhost - 让云原生时代的开发更高效
力扣练习—— 矩形区域不超过 K 的最大数值和(hard)
暑期总结4
jlink and swd interface definition
jlink 与 swd 接口定义
LeetCode 82. Remove Duplicate Elements in Sorted List II
LeetCode 21. 合并两个有序链表
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions