JSP Chinese garbled problem 1

Source: Internet
Author: User

When Using JSP, you will inevitably encounter Java Chinese garbled characters. There are different solutions for different situations. The following are the garbled issues I encountered and some solutions.


I. Garbled text caused by inconsistent page Codes

This garbled problem is the most simple garbled problem, which usually occurs for new users. See the following JSP page code:

<% @ Page Language = "Java" pageencoding = "UTF-8" %> <% @ page contenttype = "text/html; charset = iso8859-1 "%> <HTML> 

Chinese garbled characters:

Encoding appears in three places on the above page

1. pageencoding = "UTF-8 ":The function of the pageencoding attribute is to specify the encoding format (or the encoding format used for storage) of JSP pages. Java IDE (for example, myecljpse) saves files according to the encoding format. Compile the JSP file, including Chinese characters.

2. contenttype = "text/html; charsets = iso8859-1 ":The contenttype attribute specifies the content type that the server responds to the browser after a request is sent. Because pageencoding specifies that the storage format is UTF-8, but contenttype specifies that the JSP file is decoded as a iso8859-1, so if there is a Chinese certainly garbled, so the two must be consistent.

3. content = "text/html; charset = UTF-8 ":Content controls the browser's decoding method. If all the preceding decoding operations are consistent and correct, the encoding format will not be affected. However, some web pages are garbled because the browser cannot determine which encoding format to use. Because the page is sometimes embedded into the page, the browser obfuscated the encoding format. Garbled characters appear.


Chinese garbled solution:

Step 1,Unified the encoding formats of pageencoding, contenttype, and content on the JSP page. The corrected code is as follows:

<% @ Page Language = "Java" pageencoding = "UTF-8" %> <% @ page contenttype = "text/html; charset = UTF-8 "%> <HTML> 

Step 2,Set IDE (I used myeclipse) JSP page encoding format, set it to UTF-8. For specific settings, see the following link:
Eclipse http://blog.csdn.net/cxwen78/article/details/6400798

(Myeclipse) http://cavonchen.iteye.com/blog/681810


Ii. garbled characters received after the form is submitted in post Mode

<% @ Page Language = "Java" Import = "Java. util. * "contenttype =" text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <HTML> 

Running result: Enter "Chinese" as the user name. After submission, garbled characters are displayed after the "username value.


This is also a common problem. This garbled code is Tomcat's internal encoding format iso8859-1 in disorder, that is to say, when the post is submitted, if there is no set to submit the encoding format, it will be submitted in iso8859-1 mode, the accepted JSP is accepted in UTF-8 format. Cause garbled characters. For this reason, there are several solutions and comparison below.


1. encoding and conversion when parameters are accepted:String STR = new string (request. getparameter ("something"). getbytes ("ISO-8859-1"), "UTF-8"); in this way, each parameter must be transcoded. Very troublesome. However, Chinese characters can be obtained.


2. encode and convert parameters:At the beginning of the Request page, execute the request encoding code, request. setcharacterencoding ("UTF-8"), set the character set of the submitted content to the UTF-8. In this way, the page that accepts this parameter does not have to be transcoded. Use string directly
STR = request. getparameter ("something. However, this statement must be executed on each page. This method also has an effect on post submission. The enctype = "multipart/form-Data" for get submission and file upload is invalid.

The modified code is as follows:

<% @ Page Language = "Java" Import = "Java. util. * "contenttype =" text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <HTML> 

The comparison shows that the request. setcharacterencoding ("UTF-8") is added, and the running result is as follows: "Chinese" is displayed correctly"


3. encode and convert parameters:To avoid writing request. setcharacterencoding ("UTF-8") per page, we recommend that you use a filter to encode all JSPs. There are many examples on the Internet. Refer to Baidu and Google.


There are still some issues about JSP Chinese garbled characters, such as the garbled characters in form get submission methods, script code, URL requests, received parameter garbled characters, garbled characters during file uploading, and database garbled characters, to be continued...



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.