JSP pageencoding and contenttype attributes

Source: Internet
Author: User

JSP pageencoding and contenttype attributes

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, whether in notepad or ueCodeIf it 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" %>

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.