Encoding settings in JSP:
1. pageencoding: <% @ page pageencoding = "UTF-8" %>
JSP page code: Code of the JSP file
2. contenttype: <% @ page contenttype = "text/html; charset = UTF-8" %>
Code displayed on the web page: the code displayed in the browser for the JSP output stream
3. html page charset: <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
Web page input encoding: font encoding entered in the input box
4. setcharacterencoding: request. setcharacterencoding (), response. setcharacterencoding ()
Request stream input by the Web server: request data of the corresponding Web Server Browser
5. setcontenttype: Response. setcontenttype ()
Response stream output by the Web server: output data of the corresponding Web Server Browser
Their mutual influence, scope, and order of action
1. pageencoding: only indicates the encoding format of the JSP page, which has nothing to do with the encoding displayed on the page;
When the container reads (file), (database), or (String constant), it converts it to Unicode used internally.
The page content is displayed after the internal Unicode is converted to the encoding specified by contenttype;
If the pageencoding attribute exists, the character encoding method of the JSP page is determined by pageencoding,
Otherwise, it is determined by the charset in the contenttype attribute. If the charset does not exist, the character encoding method of the JSP page will be used.
The default ISO-8859-1.
2. contenttype: Specifies the MIME type and the character encoding method for JSP page responses. The default value of the MIME type is "text/html ";
The default value of the character encoding method is "ISO-8859-1". The MIME type and the character encoding method are separated by semicolons;
3. Relationship between pageencoding and contenttype:
1. The content of pageencoding is only used for encoding during JSP output and will not be sent as a header. It tells the Web Server
The encoding of the JSP page, that is, the encoding of the response stream output by the Web server;
2. In the first stage, JSP is compiled into. Java, which reads the JSP according to the pageencoding setting. The result is translated by the specified encoding scheme.
Java source code (. Java ).
3. The second stage is the compilation of Java source code from javac to Java bytecode, No matter what encoding scheme is used in JSP writing,
After this stage the results are all UTF-8's encoding Java source code. javac read with UTF-8's Encoding
Java source code, compiled into the binary code of the UTF-8 encoding (namely. Class), which is the JVM constant string in the binary code
(Java encoding.
4. The third stage is the Java binary code from Tomcat (or its application container) load and execution phase 2,
The output result is displayed on the client. In this case, the parameter contenttype hidden in phase 1 and phase 2 is effective.
4. The setting method with the same effect as contenttype is also the charset, response. setcharacterencoding (),
Response. setcontenttype (), response. setheader (); response. setcontenttype (),
Response. setheader (); the highest priority, followed by response. setcharacterencoding ();
<% @ Page contenttype = "text/html; chareset = GBK" %> and <meta http-equiv = "Content-Type"
Content = "text/html; charset = gb2312"/>.
5. enter the encoding on the web page: Specify the encoding when setting the page encoding <% @ page contenttype = "text/html; chareset = GBK" %>; if the display of the page is set to a UTF-8, all of the user's page input will be encoded by UTF-8; the server program will set the input encoding before reading the form input; after the form is submitted, the browser converts the form field value to the byte value corresponding to the specified character set, and then encodes the result byte according to the HTTP standard URL encoding scheme. but the page must tell the server the encoding method of the current page; request. setcharacterencoding (), can modify the serverlet to get the Request Encoding, response. setcharacterencoding (), which can modify the encoding of the serverlet returned results.