A detailed analysis of the two attributes of Pageencoding and contenttype in JSP pages __jsp

Source: Internet
Author: User
Tags tomcat server

<?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>

Simply put, pageencoding is the code of the JSP file itself ContentType CharSet refers to the content encoded by the server when it is sent to the client, for example: pageencoding= "GBK", which means to tell the JVM that the JSP itself uses the "GBK" code, when the JSP compiled into a servlet to the JVM, the "GBK" code to translate the JSP Web source file into a unified UTF-8 form of Java bytecode, if not set, the JVM defaults to iso-8859 this encoding. The CHARSET=GBK in contenttype indicates that the output of this page file to the browser is GBK. In this process, a JSP source file needs to go through three stages, two times coding, to complete a complete output.

An example of a three-phase, two-encoding process is illustrated by the process of outputting a JSP file to a browser and displaying it through a servlet container fake (set to Tomcat)

First stage: First encoding: from JSP to Servlet (. java file), from Pageencoding ——— >utf-8

In the first phase of the JSP to complete the first coding: the first code used in the instruction is pageencoding, according to pageencoding= "xxx" instructions to find the code of the rule for "xxx", When the server compiles a JSP file into a. java file, it reads the JSP according to the pageencoding setting, resulting in the translation of the specified encoding scheme into a unified UTF-8 Java source code (i.e.. java), if Pageencoding is not set, use the default iso-8859 encoding. Set the wrong out is the Chinese garbled.

Phase two: From the servlet file (. java) to the Java bytecode file (. Class), from the UTF-8 ——— >utf-8

The first stage is to compile the JSP into a. java file, and the second phase is primarily to translate (. java) source files into the JVM's recognizable Java bytecode, the Java bytecode file, which is the Javac command, which is what we normally call (. Class) File. In this phase, no matter what the JSP is written in the encoding scheme, through this phase of the results are all UTF-8 encoding Java source. 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). This process is determined by the internal specifications of the JVM, not outside the control, which is why, sometimes it seems garbled programs can be compiled, but to the cause of the normal display.

Phase III: Second encoding: from Tomcat server to browser, from Utf-8―――>contenttype

This phase, mainly tomcat display of the output JSP page process, which in the process of the instructions used is contenttype. Tomcat (or its application container) loading and executing is generated from the second phase Java binary code, output results, that is, the client can see the results, in this case output, by the charset of the ContentType attribute to indicate that the The binary code in the form of UTF8 is output in the form of charset encoding. If there is no artificial setting, the default is the form of iso8859-1.

Note: The above mentioned process is generally applicable in most cases. But this is not absolute, but also depends on the jspc of their own way of handling. In the web design, set pageencoding 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, if the specified pageencoding can be implemented correctly transcoding. For example: <%@ page contenttype= "Text/html;charset=utf-8"%> will print garbled, because the input "hello" encoding Way you do not specify, in most cases we use the computer in mainland China in the input "Hello" time, will use GBK to encode, but for the server, whether the right to catch "Hello" is not known. However, if you change to <%@ page contenttype= "Text/html;charset=utf-8" pageencoding= "GBK"%> then the server will be correctly caught "Hello", That's why there are two different instruction sets, and two different coding processes.

I understand these three stages and the two-time coding process, then in the programming, JSP Web pages, databases, Web servers, about the setting of some characters will be very clear, otherwise, will be set to go, the character followed, and finally did not turn the right to come, remember a little, million change in its trace, As long as the most essential things to understand, no matter how the change will not have too much problem.

Recently, the author will be the database and Web interaction in the process of some garbled problems, character set problems, coding rules, and then sorted out the next article to meet with the reader.

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.