当前位置:网站首页>一针见血式处理doGet&doPost中文乱码问题
一针见血式处理doGet&doPost中文乱码问题
2022-04-21 18:53:00 【暇光署墨】
Post请求解决方案:因为post请求是通过流的形式,所以我们设置字符输入流的编码即可解决问题;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 处理post乱码
req.setCharacterEncoding("UTF-8");
}
}
Get请求乱码的原因,浏览器将我们输入的“中文”转化成了Url编码并以这种编码格式发送给了服务器,服务器是通过“ISO-8859-1”进行解码的,因此出现乱码问题;针对此问题,我们可以先对乱码数据进行编码,转为字节数组,在通过将字节数组解码以此来解决;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get--");
// 获取数据
String userName = req.getParameter("userName");
// 对乱码数据进行编码,转为字节数组
byte[] bytes = userName.getBytes(StandardCharsets.ISO_8859_1);
// 将这季节数组解码
userName = new String(bytes,StandardCharsets.UTF_8);
// 打印输出
System.out.println(userName);
}
注:Tomcat8.0之后,已经doGet请求乱码问题解决
版权声明
本文为[暇光署墨]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45295447/article/details/124321942
边栏推荐
- 如何检测PC上插了多少个摄像机?并进行多摄像头同步录制?
- 669. 修剪二叉搜索树
- 86 R k-means,层次,EM聚类介绍
- Introduction to IPv6 foundation and common commands
- MySQL数据库学习---第六章多表查询的课后习题
- How does PR open MKV files? How can MKV files be converted to MP4 and how can MP4 be consumed by ImageJ?
- CheKine 系列高密度脂蛋白(HDL-C)含量检测方案
- Kotlin | 关于 Lazy ,你应该了解的这些事
- T-SQL language of SQL Server database
- Teach you how to build components and applications by hand
猜你喜欢

The interviewer asked me about my understanding of the transaction isolation mechanism? That's my answer

使用MCUXpresso开发RT1060(1)——驱动RGB接口LCD

Excel表格快速生成LaTeX

Which brand of wireless Bluetooth headset is good? Wireless Bluetooth headset recommendation
![[04] [02] [02] SPI mechanism](/img/62/c9852086e7893d392ef407c294dd1e.png)
[04] [02] [02] SPI mechanism

解析创客教育的实践进程

PR如何打开MKV文件?MKV文件如何转为mp4,以及MP4如何被imageJ食用?

LeetCode1765. 地图中的最高点(BFS)

Chinese NER Using Lattice LSTM 论文解读

Finally, wechat scanning code login is completed. It's really fragrant..
随机推荐
《Vite学习指南---基于腾讯云Webify部署项目》视频课程上线“云+社区”
Kotlin | these things you should know about lazy
Win10 uwp inkcanvas control data binding
cnpm|npm设置镜像地址操作
SQL 数据类型
Which wireless Bluetooth headset is easier to use? 2022 Bluetooth headset recommendations
Win10 uwp asynchronous progress bar
[JS learning notes 40] complex factory mode
美国IBM研究院Payel Das等人NMI论文:优化分子的通用型机器学习框架
Daily question series: water bottle
MySQL的吞吐量
Mysql database learning - Chapter 6 post class exercises of multi table query
【ES6】let、const、解构赋值、模板字符串
手把手教会你搭建组件与应用
零知识证明的潜在价值
Tiandan released the cloud native intelligent operation and maintenance product line and launched the world's first full stack observability technical standard
Daily question series: shuffle
ClickHouse安装配置
CVPR 2022 | a new face attribute editing framework based on transformer
TypeScript快速上手,class,public,private,extends