About JSP garbled problem

Source: Internet
Author: User
Tags dreamweaver

On the JSP garbled problem resolution.
1 The most basic garbled problem.
This garbled problem is the simplest garbled problem. General Xinhui appears. is the page encoding inconsistency caused by garbled.
<%@ page language= "java" pageencoding= "UTF-8"%>
<%@ page contenttype= "Text/html;charset=iso8859-1"%>


<title> Chinese issues </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">


<body>
I'm a good man.
</body>

Three places of code.

The first place in the encoding format is the storage format of the JSP file. Eclipse will save the file based on this encoded 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. So

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's no

In this line, "I am a good person" will also appear garbled. Must be consistent.

The third code is to control how the browser is decoded. If the previous decoding is consistent and error-free, this encoding format is not related

。 Some pages are garbled because the browser cannot determine which encoding format to use. Because pages are sometimes embedded in the page, causing

The browser confuses the encoding format. There was garbled characters.

2 The garbled problem received when the form was submitted using post

This problem is also a common problem. This garbled is also Tomcat's internal encoding format iso8859-1 in trouble, that is to say post

When committing, if the encoding format of the submission is not set, it will be submitted in iso8859-1 manner, but the accepted JSP is Utf-8

Accept. causes garbled characters. Since this is the reason, there are several workarounds and comparisons.

A encoding conversion when a parameter is 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"), put the submission within

Set 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, for Get commit and upload files when the enctype= "Multipart/form-data" is no

and effective. The following is a separate description of the two garbled cases later.

C to avoid writing request.setcharacterencoding ("UTF-8") on every page, we recommend using filters for all JSPs

for encoding processing. There are many examples of this online. Please check them yourself.

3 How the form get submitted is garbled.

If you use get to submit the Chinese language, the page that accepts the parameter will also appear garbled, this garbled reason is also the inside of Tomcat code lattice

Type iso8859-1. Tomcat encodes the kanji in the default encoding of Get, appends the encoding to the URL, causing the iso8859-1

The parameters of the accepted page are garbled/,.

Workaround:

A using the first method in the previous example, the accepted characters are decoded and then transcoded.

B get goes for URL commits, and iso8859-1 is encoded before entering the URL. If you want to influence this code, you need to

Server.xml connector node increased usebodyencodingforuri= "true"

Property configuration, you can control how Tomcat Chinese character coding The Get method, and the above property controls the get commit is also used

Encoded in the encoding format set by request.setcharacterencoding ("UTF-8"). So automatically coded as Utf-8, accept page

It is OK to accept the face normally. 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.
The. If the encoding is obtained from the URL, the Accept page is decoded according to uriencoding= "UTF-8".

4 garbled solution 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 Apach's advance Commons-fileupload.jar has
Bug, after taking out the Chinese characters to decode, 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 into garbled characters, if the number is odd, it will appear garbled
, the even number is parsed normally.
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 normal all Chinese characters as well as words
Character.

5 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 the
The Chinese characters that have to the URL must be encoded before they can be used. 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") "), the resulting 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"? The default encoding format is used when not set
Iso8859-1. 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 the even
Number, get the last character is garbled. And if the last character is in English, it will parse normally, but the Chinese
The dot symbol is still garbled. Expedient, if you have no Chinese punctuation in your parameters, you can add an English character to the value at the end of the parameter
Number to solve the garbled problem, get the parameters and then remove the most behind the symbol. can also be pooled or used.

6 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
The 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. Script
Processing the encoding is troublesome, you must have the corresponding encoding script corresponding file, and then call the method in the script to encode the Chinese characters.

7 garbled questions about JSP opening in MyEclipse
For an already existing project, the storage format of the JSP file may be utf-8. If the newly installed eclipse is turned on by default, use the
The encoding format is iso8859-1. So the JSP inside the Chinese characters appear garbled. This garbled is easier to solve, directly to
eclipse3.1 preferences found in the General-〉edidor, set to your file open code for Utf-8 can be. Eclipse will
Automatically re-opened in the new encoded format. Chinese characters can be displayed normally.
8 about HTML pages open 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

About JSP garbled problems

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.