The difference of pageencoding contenttype in JSP page

Source: Internet
Author: User
Tags tomcat

Pageencoding is the encoding of the JSP file itself

ContentType CharSet refers to the content encoding of the server when it is sent to the client

JSP to go through the "code" two times, 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 stage is JSP compiled into. Java, which reads the JSP according to the pageencoding settings, resulting in the translation of the specified coding 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 phase is from Javac Java source to the Java bytecode compiled, regardless of the JSP when the encoding scheme used, after this phase of the results are all UTF-8 encoding Java source code.

Javac uses UTF-8 's encoding to read Java source code, compiled into UTF-8 encoding binary code (that is,. Class), which is the JVM's specification for regular numeric strings expressed in binary (Java encoding).

The third stage is the Java binary Code of Tomcat (or its application container) loading and executing phase two, and the output result, which is seen at the client, is hidden in the parameters of stage one and phase two, and contenttype is effective.

ContentType's setting.

The pageencoding and ContentType presets are all iso8859-1. And randomly set one, and the other followed the same (TOMCAT4.1.27). But it's not absolute, it depends on how the JSPC are 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 by default is the operating system set by the locale corresponding to the code, such as the mainland is GBK, Taiwan is BIG5 or MS950. In general, whether 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.

However, the JSP file is not the case, it does not have this default transcoding process, but the specified pageencoding can implement the correct transcoding.

As an example:

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

Big will print out 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= "GBK"%>

This will be the server must 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.