JSP needs to be "encoded" twice. The first stage will use pageEncoding, the second stage will use utf-8 to utf-8, and the third stage is the web page produced by Tomcat, using contentType.
Regarding the difference between pageEncoding and contentType attributes in JSP pages:
pageEncoding is the encoding of the jsp file itself
The charset of contentType refers to the content encoding when the server sends it to the client.
JSP needs to be "encoded" twice. The first stage will use pageEncoding, the second stage will use utf-8 to utf-8, and the third stage is the web page produced by Tomcat, using contentType.
The first stage is to compile jsp into .java. It will read jsp according to the setting of pageEncoding. The result is that the specified encoding scheme is translated into a unified UTF-8 JAVA source code (i.e. .java). If the pageEncoding setting is wrong, Or there is no setting, and what comes out is Chinese garbled characters.
The second stage is the compilation from JAVAC's JAVA source code to java byteCode. No matter what encoding scheme is used when writing JSP, the results of this stage are all UTF-8 encoded java source codes.
JAVAC uses UTF-8 encoding to read java source code and compile it into UTF-8 encoding binary code (i.e. .class). This is the JVM's specification for the expression of constant strings in binary code (java encoding).
The third stage is that Tomcat (or its application container) loads and executes the JAVA binary code from stage two. The output result is what is seen on the client. At this time, the parameter contentType hidden in stages one and two is It worked
contentType setting.
The defaults for pageEncoding and contentType are both ISO8859-1. If you set one of them casually, the other will be the same (this is the case for TOMCAT4.1.27). But this is not absolute, it depends on the processing method of each JSPC. pageEncoding is not equal to contentType, which is more conducive to the development and display of CJKV text JSP web pages in Asia (for example, pageEncoding=GB2312 is not equal to contentType=utf-8).
The jsp file is not like .java. When .java is read by the compiler, it defaults to the encoding corresponding to the locale set by the operating system. Generally, whether we write code in Notepad or in ue, if there is no special transcoding, what is written will be the content in the local encoding format. Therefore, the method adopted by the compiler can just allow the virtual machine to obtain the correct data.
But this is not the case for jsp files. It does not have this default transcoding process, but correct transcoding can be achieved by specifying pageEncoding.
For example:
<%@ page contentType="text/html;charset=utf-8" %>
Most of them print out garbled characters, because the "How are you" I entered is from gbk, but it is not known whether the server correctly captured "How are you".
Just change it to the following content
<%@ page contentType="text/html;charset=utf-8" pageEncoding="GBK"%>