PageEncoding and ContentType attributes in java

Source: Internet
Author: User

What are the differences between the PageEncoding and ContentType attributes on the JSP page? Let's take a look:

PageEncoding:

Is the code of the JSP file.

ContentType:

The ContentType attribute specifies the HTTP content type of the response. If ContentType is not specified, the default value is text/HTML.

Charset refers to 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) 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 being read by the compiler. For example, if the Chinese mainland is GBK, Taiwan is BIG5 or MS950. 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.

However, if you change

% @ Page contentType = "text/html; charset = utf-8" pageEncoding = "GBK" %

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.