HttpServletResponse application page Chinese output garbled problem solution

Chinese output garbled problem:

Because the data in the computer is stored in binary form, when text is transmitted, the conversion between characters and bytes occurs. The conversion between characters and bytes is done through a code lookup table. The process of converting characters into bytes is called encoding, and the process of converting bytes into characters is called decoding. If the encoding and decoding are inconsistent, garbled problems will occur.

solution:

In the HttpServletResponse object, two common garbled solutions are provided:

The first way:

//设置HttpServletResponse使用utf-8编码
response.setCharacterEncoding("utf-8");
//通知浏览器使用utf-8解码
response.setHeader("Content-Type","text/html;charset=utf-8");

The second way;

//包含第一种方式的两个功能
response.setContentType("text/html;charset=utf-8");

In summary, there are two ways to solve the garbled problem of Servlet pages, usually in order to make the code more concise, the author recommends the second way.

Guess you like

Origin blog.csdn.net/weixin_43553142/article/details/105692565
Recommended