当我们从页面提交内容或显示内容时可能会出现中文乱码的现象。
我们在代码中加上下面这两行代码即可。
response.setContentType(“text/html;charset=utf-8”);
request.setCharacterEncoding(“utf-8”);
添加代码的位置如下即可:
@WebServlet(name = "UserListServlet",urlPatterns = "/userlist") public class UserListServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 加到这里即可 response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }