Why is the JSP include HTML garbled after?

Source: Internet
Author: User

JSP has two properties: Pageencoding and ContentType, the former is the encoding of the JSP file itself, and the latter charset refers to the content encoding that the server sends to the client.
So JSP to go through two "code", the first stage pageencoding, the second stage will use Utf-8 to Utf-8, the third stage is in the client browser to see the Web page, with the ContentType.

The first stage is that the JSP is compiled into. Java, which reads the JSP according to the pageencoding setting, and the result is translated by the specified encoding scheme into a unified UTF-8 Java source code (ie. java), if the pageencoding set wrong, or not set, out of the Chinese garbled.
The second stage is compiled by Javac Java source code to Java bytecode, no matter what coding scheme is used when JSP is written, the result of this stage is all UTF-8 encoding Java source code. Javac uses UTF-8 's encoding to read Java source code, compiled into a UTF-8 encoding binary (i.e.. Class), which is the JVM's specification of a constant number string expressed within a binary code (Java encoding).
The third stage is Tomcat (or its application container) loading and executing phase two of the Java binary code, the output of the results, that is, seen on the client, at this time hidden in phase one and phase two of the parameters contenttype to play a function, The include directive is executed before the first stage, and note that this is before the first stage, so if the file containing the file and the contained file is encoded not utf-8, then the instruction will not work properly, and the contained file cannot be correctly converted from the original encoding to the encoding of the containing file. There will be garbled phenomenon.

There are many ways to solve this, a very simple way is to set the pageencoding of the containing file to Utf-8, while the pageencoding of the included file is also set to Utf-8, if the included file is HTML, If the IDE you are using is eclipse, you can do so by right-clicking on the included file name, selecting Properties, Resource, selecting Utf-8 in the text file encoding, This will do the normal display. If you are using other editing tools, such as Dream Weaver, there is also a place to change the page encoding in response.
Another way to compare the trouble point, but also very useful, support a variety of coding methods, only need to modify the Web. xml file.

The method is: Add in Web. XML (Note that I am using Java EE version 2.5 for
<web-app xmlns:xsi= "" xmlns= "" xmlns:web= "" xsi:schemalocation= "" id= "webapp_id" version= "2.5〃>)

<jsp-config>
<jsp-property-group>
&LT;DESCRIPTION&GT;JSP encoding example</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>gbk</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude></include-prelude>
<include-coda></include-coda>
</jsp-property-group>
<jsp-property-group>
<description>html encoding example</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>*.html</url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>gbk</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude></include-prelude>
<include-coda></include-coda>
</jsp-property-group>
</jsp-config>

It is important to note that the page-encoding set here must be the same as the real contained file and the pageencoding containing the file, and the method of modification is shown in the previous method.

Why is the JSP include HTML garbled after?

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.