get passed Chinese garbled summary of solution

1 basic garbage problem.

The garbage problem is the simplest garbage problem. Most of the new will appear. Page is garbled coding inconsistencies caused.

<%@ page language="java" pageEncoding="UTF-8"%>

<%@ page contentType="text/html;charset=iso8859-1"%>

<html>

<head>

<Title> Chinese problem </ title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

</head>

<body>

I am a good person

</body>

</html>

Encoding three places.

The first place encoding format for the storage format jsp files. Eclipse will save the file according to the encoding format. And compile jsp file

Including inside the characters.

The second encoding format to decode. UTF-8 is stored as a file is decoded into iso8859-1, certainly so if the Chinese distortion. It

It must be consistent. The second place where this line, you can not. The default is to use iso8859-1 encoding format. So if there is no

This line of words, "I am a good person" will appear garbled. It must be consistent.

The third coded at the browser control decoding method. If you are consistent and in front of decoding is correct, this encoding format does not matter

. Some pages garbled, because the browser can not determine which encoding format. Because the pages are sometimes embedded in the page and causes

Browser confused encoding format. Garbled.

2 form using the garbage problem received after the submission of Post

The issue is also a common problem. This is garbled tomcat internal iso8859-1 encoding format in trouble, that is to say post

Submitting, if no encoding format submitted, will be submitted iso8859-1 manner, but in a manner acceptable jsp utf-8 of

accept. Cause garbled. Since this is the reason, here are a few solutions, and compare.

Transcoding accept parameter A

String str = new String(request.getParameter("something").getBytes("ISO-8859-1"),"utf-8")

; In this case, each of such parameters must be transcoded. A lot of trouble. But you can get Chinese characters.

B at the beginning of the page request, encoded code execution request, request.setCharacterEncoding ( "UTF-8"), submitted within the

Yung character set to UTF-8. In this case, to accept this argument you do not have to turn the page on the yard. Use directly

String str = request.getParameter ( "something"); parameter characters can be obtained. But each page need to perform this sentence.

This approach will have the effect of post submitted for enctype = "multipart / form-data" at the time of submission and get uploading files is not

Effective. The following individual circumstances of the two garbled again be explained later.

C To avoid page should write request.setCharacterEncoding ( "UTF-8"), recommend the use of filters for all jsp

The encoding process. The Internet has many examples. Please own review.

3 garbled form get submitted handled the way.

If you use the Chinese get submission, the page will also accept parameters garbled, garbled this reason is tomcat internal encoding format

Type iso8859-1 lead. Tomcat will get the default encoding iso8859-1 encoding the characters, encoded appended to the url, resulting in

Parameter accepts the resulting page is garbled / ,.

Solution:

A use of the embodiment in a first embodiment, received characters are decoded sub-code.

B Get taking the url submission, but before entering the url has been iso8859-1 encoding process is. To affect this code you need to

Connector server.xml of nodes increases useBodyEncodingForURI = "true"

Property configuration, you can control the tomcat of the Chinese character encoding get in the way, get control of this property above is submitted by

Encoding format request.setCharacterEncoding ( "UTF-8") set encoding. Therefore, encoded as automatically utf-8, pages accepted

Plane normal to accept it. But I think the real encoding process, tomcat but also according to

<Connector port="8080"

maxThreads="150" minSpareThreads="25" maxSpareThreads="75"

enableLookups="false" redirectPort="8443" acceptCount="100"

debug="0" connectionTimeout="20000" useBodyEncodingForURI="true"

disableUploadTimeout="true" URIEncoding=”UTF-8”/>

URIEncoding = "UTF-8" which set a further coding, but since has been encoded as utf-8, will not be re-encoded variant

melted. If the url is obtained from encoding based on the accepted page URIEncoding = "UTF-8" to decoding.

4 to solve garbled when uploading files

When uploading files, form a form set is enctype = "multipart / form-data". In this way submission to stream.

If you are using apach upload component, you will find a lot of garbage imagination. This is because apach upfront commons-fileupload.jar have

bug, after removing characters is decoded, as the author of this embodiment, the encoding and automatically uses the tomcat default encoding format iso-8859-1

. But the problem is garbled: periods, commas, and other special symbols into a garbled, if the number of characters is odd, garbled appears

, The even number is resolved correctly.

    Solution: Download commons-fileupload-1.1.1.jar this version of the jar has solved these bug.

There remains a need for characters taken from iso8859-1 to utf-8 when taken transcoded content. We have been able to get all the normal characters and words

symbol.

5 Java code that distortion on the request url accept parameters

url encoding format, depending on the above mentioned URIEncoding = "UTF-8". If you set the encoding format, it means that the

Chinese characters have to url parameters, it must be encoded before they can. Otherwise the characters are garbled parameter values ​​obtained, for example,

A link Response.sendDerect ( "/ a.jsp name = Zhang Dawei?"); And used directly inside a.jsp

String name "); get is garbled because of the provisions that must be utf-8 can, therefore, this shift should be written:

    Response.sendDerect ( "/ a.jsp name = URLEncode.encode (?" Zhang Dawei "," utf-8 "); can.

If you do not set this parameter URIEncoding = "UTF-8", what will happen? Is not set to use the default encoding format

iso8859-1. Problem came out of the first parameter value is the number if the number is odd, it can be resolved properly if the even

The number, get the last character is garbled. There is, if the last character if it is in English, then you can normally resolve, but the Chinese standard

Point symbol still appears garbled. Expediency, if your argument is not Chinese punctuation, you can finally add an English character in the parameter value

Number to solve the garbage problem, then get rid of this parameter rearmost symbol. You can also join or use.

6 script code on url request parameters received garbled

The script will be controlled page turn, it will involve incidental parameters, page parsing and acceptance of this parameter. If this

Character parameters are not URIEncoding = "UTF-8" specified by the encoding process, the received characters are accepted page distortion. script

Coding process is too much trouble, there must be a corresponding encoding the corresponding script file, and then call the method in the script of Chinese characters can be encoded.

7 garbled question on jsp opened in MyEclipse

For an existing project, Jsp file storage format may be utf-8. If the new eclipse, use the default open

Encoding format is iso8859-1. Thus resulting in garbled characters jsp inside. The garbled relatively easy to solve, directly to the

eclipse3.1 preferences inside to find general-> edidor, set to open your file can be encoded as utf-8. Eclipse will

Automatic re-open in a new encoding format. Chinese characters can be displayed properly.

About 8 html page open garbled situation in eclipse

As most of the pages are produced by dreamweaver, its storage format with the recognition of difference leads to eclipse.

Generally this situation, create a new jsp in eclipse, copy and paste page content directly from dreamweaver to jsp can be.
////////////////////////////////////////////////// ////////////////////////////////////////

Reproduced in: https: //www.cnblogs.com/Codenewbie/p/3336538.html

Guess you like

Origin blog.csdn.net/weixin_34040079/article/details/93448113