Java Web Coding Problem One: The coding problem of JSP

Source: Internet
Author: User

First, let's look at a header for the JSP file generated by eclipse:

Figure 1:jsp File encoding

as shown in the JSP three encoding,

    1. ① represents the encoding of the server in response to client requests. The server uses it to set the HTTP response header's Content-type. It's like using Response.setcontenttype ("Text/html;charset=utf-8") in a servlet. You can see it in the Content-type in the header of the browser.

    2. ②pageenconding encoding is the encoded format in which the value JSP file itself is encoded. Because the JSP will eventually be compiled into a CLSS bytecode, and the bytecode is Unicode encoded, you must know the format to be converted. If not set, eclipse encodes it by iso-8859-1 encoding. If 1 is not set, then the 2pageEncoding encoding is used to set it.

    3. The Content in ③ is also set to the browser's decoding format, but its priority does not have a high content-type priority in the header, so as long as the Content-typeis set in the header, Then the content of meta is not used in the basic.

Here's a very simple example to test:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Very simple, run without garbled. Use the Firefox debug to look at the request header as follows:

Figure 2:header1

If you are interested, you can try to Figure 1 ① encoding contenttype removed, found still no garbled, look at the response header information, Content-type encoding is still utf-8, This is because if you do not set the encoding of ① in Figure 1 contenttype the default is to use the ②pageencoding encoding in Figure 1.

You can also try to change the encoding in the ③ to iso-8859-1 and other incompatible with the Chinese encoding, found still not garbled, view the request header information Content-type encoding is still utf-8, And the priority of encoding in meta is higher, so the browser still uses the Utf-8 in the response header to decode.

Now we change the encoding contenttype encoding of ① to GBK, for example:


Figure 3:CONTENTTYPE-GBK

In Firefox debug check the request header information Content-type encoding, found that Content-type encoding is GBK so that ① encoding contenttype is set in the response header content_type. But Strange is not garbled, the server is clearly utf-8 code, the response head is GBK Why did not appear garbled it? No solution for the moment.

But we put in ① encoding contenttype code to iso-8859-1 incompatible with the Chinese encoding method, found that there is garbled.

There is no garbled reason in Figure 3, perhaps we can find some hints from the browser decoding process, please refer to:

Www.w3.org/html/wg/drafts/html/master/syntax.html#parsing-with-a-known-character-encoding


Java Web Coding Problem One: The coding problem of JSP

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.