public class Servlet01 extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 让浏览器使用 utf-8 解码
resp.setContent("text/html;charset=utf-8");
// 通过指定 (请求体) 编码格式解决
req.setCharacterEncoding("utf-8");
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
? 1.编码不统一(基本上都是因为这个)
? 2.数据丢失
可能产生的原因:浏览器默认使用 iso-8859-1 解码
// 让浏览器使用 utf-8 解码
resp.setContent("text/html;charset=utf-8");
可能的原因: getParameter() 默认使用 iso-8859-1编码获取的参数
// 通过指定 (请求体) 编码格式解决
req.setCharacterEncoding("utf-8");
// get 请求不需要解决
useUnicode=true&chaeracterEncoding=utf8
原文:https://www.cnblogs.com/zhiwenxi/p/11568852.html