Servlet common solution to the garbage problem
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);
}
}
Garbled reasons:
1. The encoding is not uniform (basically because of this)
2. Data Loss
servlet response garbled
Possible causes: The browser defaults to using iso-8859-1 decoding
// 让浏览器使用 utf-8 解码
resp.setContent("text/html;charset=utf-8");
servlet request garbled
Possible causes: getParameter () default parameters using iso-8859-1 encoding acquired
// 通过指定 (请求体) 编码格式解决
req.setCharacterEncoding("utf-8");
// get 请求不需要解决
Database garbled
- Url when the connection is set to:
useUnicode=true&chaeracterEncoding=utf8
- When the database creation is set to utf-8 encoding