The Web running console outputs garbled code to solve the summary, and the web Console outputs garbled code.
<! DOCTYPE html>
1 @WebServlet(name = "AServlet",urlPatterns = "/AServlet") 2 public class AServlet extends HttpServlet { 3 public void doPost(HttpServletRequest request, HttpServletResponse response) 4 throws ServletException, IOException { 5 request.setCharacterEncoding("utf-8"); 6 String username = request.getParameter("username"); 7 response.setContentType("text/html;charset=utf-8"); 8 PrintWriter out = response.getWriter(); 9 out.print(username);10 System.out.println(username);11 }12 13 protected void doGet(HttpServletRequest request, HttpServletResponse response)14 throws ServletException, IOException {15 String name = request.getParameter("username");16 response.setContentType("text/html;charset=utf-8");17 PrintWriter out = response.getWriter();18 out.print(name);19 System.out.println(name);20 }21 }1. Running Environment: Operating System: Mac OS X10.12.6, language: English Development Tool: IntelliJ IDEA 2017.2.2, default code: UTF-8Tomcat: 9.0.0.M262, problem: when running Web project, garbled characters are output on the console. 3. Solution: Set the Servlet VM options (Virtual Machine options) to:-Dfile. encoding = UTF-8
Use: chrome Version 60.0.3112.113. Result: The console output is normal.
Use: QQBrowser Version 4.2.4761.400 (64-bit). Result: The console output is normal.
Use: Safari Version 10.1.2 (12603.3.8). Result: The console output is normal.
Use: Firefox 55.0.3 (64-bit). Result: The console output is normal.
If the OS is replaced with Chinese characters, the results are consistent and the reasons for the system language are excluded.
Note: If you use the doPost () method to obtain the form, you must set request. setCharacterEncoding ("UTF-8") before obtaining the parameter; otherwise, garbled characters will appear on the console.
Reference: http://blog.csdn.net/rznice/article/details/41006913