JSP requires two "encodings". pageEncoding is used in the first stage, UTF-8 to UTF-8 is used in the second stage, and the third stage is the webpage from Tomcat, with contentType.
Differences between the pageEncoding and contentType attributes on the JSP page:
PageEncoding is the code of the jsp file.
The charset of contentType indicates the content encoding when the server sends a message to the client.
JSP requires two "encodings". pageEncoding is used in the first stage, UTF-8 to UTF-8 is used in the second stage, and the third stage is the webpage from Tomcat, with contentType.
The first stage is jsp compilation. java, it will read jsp according to pageEncoding settings, the result is translated by the specified encoding scheme into a unified UTF-8 JAVA source code (that is. java). If pageEncoding is set incorrectly or is not set, Chinese garbled characters are displayed.
The second stage is the JAVA source code of JAVAC to the compilation of java byteCode, No matter what encoding scheme is used in JSP writing, after this stage the results are all the java source code of the UTF-8 encoding.
JAVAC uses the UTF-8's encoding to read the java source code and compile it into the UTF-8's encoding binary code (that is,. class), which is the JVM's specification for the constant string expression in the binary code (java encoding.
The third stage is the JAVA binary code loaded and executed by Tomcat (or its application iner INER) in Stage 2. The output result is displayed on the client, in this case, the parameter contentType hidden in phase 1 and phase 2 is effective.
ContentType settings.
The presets of pageEncoding and contentType are both ISO8859-1. if you set either of them, the other will be the same (TOMCAT4.1.27 is the same ). but this is not absolute. It depends on the JSPC processing method. while pageEncoding is not equal to contentType, it is more advantageous for the development and display of the text CJKV series JSP webpage in Asia region (for example, pageEncoding = GB2312 is not equal to contentType = UTF-8 ).
Unlike. java, jsp files use the encoding of locale set by the operating system by default when they are read by the compiler. Generally, we write code in notepad or ue. If the code is not specially transcoded, it is written in local encoding format. Therefore, the method adopted by the compiler can give the virtual machine the correct information.
But this is not the case for jsp files. It does not have this default transcoding process, but with pageEncoding specified, transcoding can be implemented correctly.
For example:
<% @ Page contentType = "text/html; charset = UTF-8" %>
Garbled characters are printed in the metropolis, because the entered "hello" is gbk, but it is unknown whether the server correctly captures "hello.
Change to the following content:
<% @ Page contentType = "text/html; charset = UTF-8" pageEncoding = "GBK" %>