Pageencoding and ContentType properties in a JSP

Source: Internet
Author: User
Tags tomcat

  about the difference between pageencoding and contenttype two properties in JSP pages:

Pageencoding is the encoding of the JSP file itself

ContentType refers to the content encoding that the server sends to the client

JSP takes two times "Code", the first phase will use Pageencoding, the second phase will be used Utf-8 to Utf-8, the third stage is from Tomcat out of the Web page, with the ContentType. The first phase of the

is JSP compiled into. Java, which reads the JSP according to the pageencoding settings, resulting in the translation of the specified encoding scheme into a unified UTF-8 Java source code (that is, Java), if the pageencoding set wrong, or did not set, out is the Chinese garbled. The second stage of the

is compiled by Javac Java source code to the Java bytecode, regardless of what coding scheme is used in JSP writing, the results of this phase are all UTF-8 encoding Java source code.

Javac reads Java source code with UTF-8 encoding and compiles it into the UTF-8 encoding binary code (that is,. Class), which is the specification that the JVM expresses in binary code (Java encoding) for the constant number string. The third phase of

is the Java binary Code of Tomcat (or its application container) loading and executing phase two, and the output results, which are seen on the client side, The parameters that are hidden at stage one and phase two contenttype the effect of the

ContentType setting.

The Presets for the

Pageencoding and ContentType are all iso8859-1. Instead of setting one of them, the other is the same (TOMCAT4.1.27). But it's not absolute, it depends on how each JSPC is handled. And pageencoding is not equal to ContentType, more favorable for the Asian text CJKV JSP Web page development and display, (example pageencoding=gb2312 not equal to Contenttype=utf-8). The

JSP file does not resemble. Java,.java is read by the compiler defaults to the operating system set by the locale corresponding to the code, such as the mainland is GBK, Taiwan is BIG5 or MS950. and aWhether we write code in Notepad or UE, if we don't have a special transcoding, we write the contents of the local coded format. So the compiler uses the right method to get the correct data for the virtual machine.

But this is not the case for a JSP file, which does not have the default transcoding process, but specifies the pageencoding to implement the correct transcoding.

For example:

<%@ page contenttype= "Text/html;charset=utf-8"%>

Will print garbled, because the input "Hello" is GBK, but the server is correctly caught "Hello" unknown.

But if you change to

<%@ page contenttype= "Text/html;charset=utf-8" pageencoding= "G" BK "%>

So that the server will be correctly caught" hello ".

Related Article

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.