The problem of JSP encoding in Chinese

Source: Internet
Author: User

This garbled problem is the simplest garbled problem. General Xinhui appears. is the page encoding inconsistency caused by garbled.

  1. <%@ page language="java" pageencoding="UTF-8"%>
  2. <%@ page contenttype="text/html;charset=iso8859-1"%>
  3. <html>
  4. <head>
  5. <title> Chinese question </title>
  6. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
  7. </head>
  8. </head>
  9. <body>
  10. I'm a good man.
  11. </body>
  12. </html>

Three places of code

The first place in the encoding format is the storage format of the JSP file. Ecljpse will save the file based on this encoding format. and compile the JSP file, including the Chinese characters inside.
The second encoding is the decoding format. Because the file saved as UTF-8 is decoded to iso8859-1, so the Chinese must be garbled. That must be the same. And the second place in this line, can not. The default is also the encoding format using ISO8859-1. So if there is no such a line, "I am a good person" will also appear garbled. Must be consistent.
The third code is to control how the browser is decoded. This encoding format is not related if the previous decoding is consistent and error-free. Some pages are garbled because the browser cannot determine which encoding format to use. Because pages are sometimes embedded in the page, the browser confuses the encoding format. There was garbled characters.

The garbled problem received when the form was submitted using post

This problem is also a common problem. This garbled is also tomcat internal encoding format iso8859-1 in trouble, that is, when the post submission, if not set the encoding format of the submission, it will be submitted in iso8859-1 manner, the accepted JSP is Utf-8 accepted. causes garbled characters. Since this is the reason, there are several workarounds and comparisons.
A, encoding conversion when parameters are accepted
String str = new String (Request.getparameter ("something"). GetBytes ("Iso-8859-1"), "Utf-8"); In this case, each parameter must be transcoded in this way. Very troublesome. But you can actually get the kanji.
B, at the beginning of the request page, execute the requested encoding code, request.setcharacterencoding ("UTF-8"), and set the character set of the submission to UTF-8. In this case, the page that accepts this parameter does not have to be transcoded. Direct use
String str = request.getparameter ("Something"), the Chinese character parameter can be obtained. But every page needs to execute this sentence. This method is also effective for post submissions, which is not valid for enctype= "Multipart/form-data" when a get commits and uploads a file. The following is a separate description of the two garbled cases later.
C, in order to avoid writing request.setcharacterencoding ("UTF-8") on every page, it is recommended that all JSPS be encoded with a filter. There are many examples of this online. Please check them yourself.

How to handle the form get submitted in a garbled way

If you use get to submit the Chinese language, the page that accepts parameters will also appear garbled, this garbled reason is also tomcat internal encoding format iso8859-1 caused. Tomcat will encode the kanji with the default encoding of GET, append to the URL after encoding, and result in the iso8859-1 of the parameters on the receiving page.
Workaround:
A, using the first method in the above example, the accepted characters are decoded and then transcoded.
B,get is a URL submission, and has been iso8859-1 encoded before entering the URL. To affect this encoding you need to add usebodyencodingforuri= "true" to the connector node of the Server.xml
Property configuration, you can control how Tomcat Chinese character coding The Get method, which controls that the get commits are encoded using the encoding format set by request.setcharacterencoding ("UTF-8"). So automatically encoded as utf-8, accept the page to accept the normal. But I think the real coding process is that Tomcat is also based on
<connector port= "8080"
maxthreads= "minsparethreads=" maxsparethreads= "75"
Enablelookups= "false" redirectport= "8443" acceptcount= "100"
debug= "0" connectiontimeout= "20000" usebodyencodingforuri= "true"
Disableuploadtimeout= "true" uriencoding= "UTF-8"/>
The uriencoding= "UTF-8", which is set inside, is encoded again, but the encoding is not changed because it is encoded as utf-8. If the encoding is obtained from the URL, the Accept page is decoded according to uriencoding= "UTF-8".

Garbled resolution when uploading a file

When uploading a file, the form form is set to Enctype= "Multipart/form-data". This way, the file is submitted in a streaming manner. If you use the Apach upload component, you will find a lot of garbled imagination. This is because the Apach of the early Commons-fileupload.jar has a bug, take out the Chinese characters after decoding, because this way to commit, encoding and automatically use the Tomcat default encoding format iso-8859-1. But the garbled problem is: period, comma, and other special symbols become garbled, if the number of Chinese characters is odd, it will appear garbled, even the analytic normal.
Workaround: Download Commons-fileupload-1.1.1.jar This version of the jar has been resolved by these bugs.
However, you still need to transcode the extracted characters from iso8859-1 to Utf-8 when you remove the content. have been able to get all the characters as well as normal.

Java code about URL request, accept parameter garbled

The encoding format of the URL depends on the uriencoding= "UTF-8" described above. If this encoding format is set, it means that all Chinese character parameters to the URL must be encoded. Otherwise the obtained Chinese character parameter value is garbled, for example
A link response.sendderect ("/a.jsp?name= Zhang Dawi"), and in a.jsp directly use
String name = Request.getparameter ("name"); get is garbled. Because the rules must be utf-8, so this turn should be written like this:
Response.sendderect ("/a.jsp?name=urlencode.encode" ("Zhang Dawi", "utf-8");
What happens if you do not set this parameter uriencoding= "UTF-8"? If not set, the default encoding format iso8859-1 is used. The problem comes out again, the first is the number of parameter values if it is an odd number of numbers, it can be parsed normally, if you make an even number of numbers, get the last character is garbled. There is also if the last character if it is in English, it will be able to parse normally, but Chinese punctuation is still garbled. Expedient, if your parameters do not have Chinese punctuation, you can add an English symbol at the end of the parameter value to solve the garbled problem, get the parameters and then remove the most behind the symbol. can also be pooled or used.

Script code about URL request, accepted parameter garbled

The script also controls the page steering, as well as the accompanying parameters and the case where the page is parsed for the parameter. If this Chinese character parameter does not carry on the uriencoding= "UTF-8" the encoding processing, then accepts the page to accept the Chinese character also garbled. The script processing code is troublesome, must have the corresponding encoding script corresponding file, then calls the script the method to encode the Chinese character.

On the garbled problem of JSP opening in MyEclipse

For an already existing project, the storage format of the JSP file may be utf-8. If the new eclipse is installed, the encoding format used by default is iso8859-1. So the JSP inside the Chinese characters appear garbled. This garbled is easier to solve, go directly to eclipse3.1 preferences inside find General-〉edidor, set to your file open code for Utf-8 can. Eclipse will automatically re-open in the new encoded format. Chinese characters can be displayed normally.

About HTML pages opening in Eclipse garbled condition

Since most of the pages are made by Dreamweaver, their storage format differs from Eclipse's recognition.
In this case, create a new JSP in Eclipse and paste it directly from the Dreamweaver copy page content to the JSP.

The problem of JSP encoding in Chinese

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.