只需要在dopost或doget方法中添加如下语句即可
request.setCharacterEncoding("UTF-8");如:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); String user= request.getParameter("username"); System.out.println(user); }
其中下面这一句是为了使从服务器输出到客户端的汉字不至于乱码
response.setContentType("text/html;charset=UTF-8");