前端页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>登录</title>
</head>
<body>
<%@ page pageEncoding="UTF-8" %>
<div style="text-align: center"><form action="${pageContext.request.contextPath}/login" method="post">账号:<input type="text" name="username"><br>密码:<input type="password" name="password"><br>爱好:<input type="checkbox" name="hobbies" value="学习"> 学习<input type="checkbox" name="hobbies" value="游戏"> 游戏<input type="checkbox" name="hobbies" value="看剧"> 看剧<input type="checkbox" name="hobbies" value="代码"> 代码<br><input type="submit"></form>
</div>
</body>
</html>
后端获取 value 出现乱码
req.setCharacterEncoding("utf-8");resp.setCharacterEncoding("utf-8");String username = req.getParameter("username");String password = req.getParameter("password");String[] hobbies = req.getParameterValues("hobbies");PrintWriter writer = resp.getWriter();if (hobbies != null){for (int i = 0; i < hobbies.length; i++) {writer.println(hobbies[i]);}}writer.println(username + "\n" + password);

解决方法
//作用是指定服务器响应给浏览器的编码。同时,浏览器也是根据这个参数来对其接收到的数据进行重新编码(或者称为解码)。resp.setContentType("text/html;charset=utf-8");
//设置如下编码也可以resp.setCharacterEncoding("utf-16");resp.setCharacterEncoding("gbk");

参考
(10条消息) request和response的setCharacterEncoding()方法_codingCoge的博客-CSDN博客_request.setcharacterencoding
https://blog.csdn.net/qq_38409944/article/details/81327414















![[XP虚拟机安装]VMware安装XP虚拟机](https://img-blog.csdnimg.cn/bffd5f66e8d74d83a8cbef0da513ff0f.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBASmF5ZXRjaGVsbG90,size_20,color_FFFFFF,t_70,g_se,x_16)
