Summary of Chinese garbled characters in Java development

Source: Internet
Author: User

1.jsp page Content Display garbled

This garbled reason is very simple, the general tool or decoding program for Chinese characters in the resolution of the default decoding method:

<%@ page contenttype="text/html; Charset=iso-8859-1 "%>

We just need to modify its encoding, as follows:

<%@ page contenttype="text/html; Charset=utf-8 "%>

Character set: UTF-8 > GBK > GB2312

2.jsp and Servlet jumps appear Chinese garbled 2.1:method= "Post"

JSP in the form form ation= "Xxxservlet", method= "post", after the submission of the form often found that the Chinese property values in the Servlet gets garbled.

At this point you need to navigate to the DoPost () method, first add the following code to the first line in the method:

Request.setcharacterencoding ("UTF-8");

This means that the code for the set request is "UTF-8" and is generally consistent with the JSP page

Then add the code:

Response.setcharacterencoding ("UTF-8");

Response.setcontenttype ("Text/html;charset=utf-8");

This means that the encoding of the set response is "UTF-8", that is, the code of the Servlet callback JSP, the above two paragraphs can be used, it is the key to maintain consistency.

2.2:method= "Get"

JSP in the form form ation= "Xxxservlet", method= "get", the submission of the form often found that the Chinese property values in the Servlet gets garbled.

The installation directory for Tomcat to be located at this time%tomcat%/conf/server.xml file

Look for the following code snippet:

1. <connector port= "8080" protocol= "http/1.1"

2. maxthreads= "150"

3. connectiontimeout= "20000"

4. redirectport= "8443"

5. uriencoding= "UTF-8"/>

Manually add uriencoding= "UTF-8"

3. JavaScript URL pass parameter Chinese garbled problem

Programme I

HTML page:

function Testone () {
var url = "testtwo.action?expr=" + Hello;

window.location.href = encodeURI (URL);

}

Background Java code:

String expr = new String (

Request.getparameter ("expr"). GetBytes ("Iso-8859-1"), "UTF-8");

Scenario Two

HTML page:

function Testtwo () {

var url = "testtwo.action?expr=" + Hello;

Window.location.href= encodeURI (encodeURI (URL));

}

Background Java code:

String expr = Java.net.URLDecoder.decode (Lrequest.getparameter ("expr"), "UTF-8");

If the use of the WebLogic server, the second solution can be solved (my WebLogic version is WebLogic 9.2), the solution is not.

If it is a Tomcat server, these two programs can be, or in the pass parameters are not processed, the background with

String expr = new String (Request.getparameter ("expr"). GetBytes ("Iso-8859-1"), "UTF-8");

is also possible.

4. Java Web download file filename garbled

The first type: set

Response.setheader ("Content-disposition", "attachment; Filename= "+ java.net.URLEncoder.encode (fileName," UTF-8 "));

Here the file name is encoded in UTF-8 format, there will be no URL error. IE6 Note that Chinese characters cannot exceed 17 characters.

The second type: set

Response.setheader ("Content-disposition", "attachment;filename=" + New String (Filename.getbytes ("gb2312"), " Iso8859-1 "));

The Chinese name is encoded as iso8859-1. However, this encoding only supports simplified Chinese.

According to the way of appeal, we can combine the two ways to solve the majority of Chinese problems.

FileName = Urlencoder.encode (filenamesrc, "UTF-8");

if (Filename.length () >150)//Resolve IE 6.0 Bug {

Filename=new String (filenamesrc.getbytes ("GBK"), "iso-8859-1");

Response.setheader ("Content-disposition", "attachment;filename=" + filename);

}

Summary of Chinese garbled characters in Java development

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.