The Web running console outputs garbled code to solve the summary, and the web Console outputs garbled code.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.