Ajax return Chinese garbled, servlet display garbled, post submitted garbled problem, coding a big sum

Source: Internet
Author: User
Tags header net php and return string client
Ajax|servlet| Code | problem | show | chinese | garbled

Ajax return Chinese garbled problem solving
Use AJAX to get server data back to the client, the Chinese garbled. Specify codepage=936 in a previous AJAX application, and all page encodings are specified as GB2312 to be resolved.
This application has no effect, after many experiments, customer service end of the code should be absolutely no problem. Can judge the problem must be out on the server side. Just a little analysis. Although the server side specifies the file encoding format, the server output stream becomes a suspect object. Because of the use of Ms SQL2000, Unicode encoding, so the return of data to the client will appear Unicode encoded in Chinese on the GB2312 encoding page display problem. Then add a header to the server-side output stream:

Adding <%response.charset = "GB2312"%> in ASP program solves this problem.
Similarly, writing in PHP and JSP programs is similar
Php:header (' content-type:text/html;charset=gb2312 ');
JSP:response.setHeader ("Charset", "GB2312");

Ajax generated garbled reasons for the following
1, Xtmlhttp returned the data default character encoding is Utf-8, if the client page is gb2312 or other encoded data will produce garbled
2, Post method submission data default character encoding is utf-8, if server-side is gb2312 or other encoded data will produce garbled
Solutions include:
1, if the client is gb2312 code, then specify the output stream code on the server
2, server-side and client are using UTF-8 encoding

JSP, Ajax uses post to submit Chinese garbled problem solving


/*********** I original, welcome reprint, Reprint please keep my information *************/
Author: Wang Li-Mun (WALLIMN)
Email: wallimn@sohu.com
Blog: http://wallimn.bokee.com
Http://blog.csdn.net/wallimn
Time: 2006-11-15
/*********** I original, welcome reprint, Reprint please keep my information *************/


Today finally solved the problem of Ajax Chinese garbled, write an article to help friends with the same problem. My development environment: XP, Eclipse, using GB18030 encoding.
When encountered this problem, went to the internet to check a lot of articles, mentioned several solutions, such as: Total station UTF-8 code; Request headers encoded in Chinese; Use the escape function in JavaScript.
When using get method to submit data, Chinese problem is solved very well, setrequestheader ("Content-type", "text/html; encoding=gb18030") is OK. However, this method does not work in post mode. We all know that the get-way submission data has a length limit, and sometimes we have to submit the data using the Post method.
However, for the Post method, using the above several methods have been tested several times, the problem remains. I was depressed for several days.
Today to solve the problem, very simple, is to use escape (or encodeURI, two functions JavaScript function, the function is basically the same, you can check the relevant help), but to use two times, this is the key to solve the problem.
My example involves two pages, one is the initial page, the other is the AJAX request processing page.
The initial page content is as follows (hello.jsp):
/////////////////////////////////////////////////////////////////////////////////////
<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%>
<%string path = Request.getcontextpath ();%>



ajax Submit Page







/////////////////////////////////////////////////////////////////////////////////////
The contents of the AJAX Request Processing page (act.jsp) are as follows:
/////////////////////////////////////////////////////////////////////////////////////
<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%>
<%string path = Request.getcontextpath ();%>

<% @page import= "Java.net.URLDecoder"%>


ajax deal





<%
Traverses the output parameter contents.
For (Enumeration E = Request.getparameternames (); e.hasmoreelements ();) {
String h = (string) e.nextelement ();
String v = request.getparameter (h);
String mm = Java.net.URLDecoder.decode (V, "UTF-8");
SYSTEM.OUT.PRINTLN ("Request parameter:" + H + "=" + mm);
}
%>


/////////////////////////////////////////////////////////////////////////////////////
Analysis: When the Request.getparameter () function is invoked, the decoding process of the URI is automatically performed, and the decoding process built into the call will cause garbled characters to appear. After the URI is encoded two times, the Request.getparameter () function gets the content that the original information URI encodes once. The Java.net.URLDecoder.decode () can be used to solve the original correct information by using the controllable decoding function.
The above analysis is purely personal opinion, I do not know whether it is correct.


========================
Finally, the servlet problem.

Oh, kung fu is not negative!! Finally figured out, very simple:
1 JSP as long as the addition of <%@ page contenttype= "text/html;charset=gb2312" language= "java"%> can be correctly submitted in Chinese!
2 in the servlet, respectively, the
1) response.setcontenttype ("text/html; charset=gb2312 ");
2) request.setcharacterencoding ("gb2312");
Can be the correct output, and read Chinese!!
So the problem now is that if you use struts, the servlet he gave us didn't join
1) response.setcontenttype ("text/html; charset=gb2312 ");
2) request.setcharacterencoding ("gb2312");
So we can't read Chinese correctly when he calls our actionform, and we can only write action, and we can't.
So how do you modify the servlet? In the study!
Thank you, have a good suggestion to continue OH
====================
transcoding code
public static string ISO2GB (String str) {
if (str== "") {
Return "";
}

String result = "";

try {
byte[] tmp = str.getbytes ("Iso8859_1");
result = new String (tmp, "UTF-8");
catch (Exception e) {
System.out.println (e);
}

return result;
}
===========================


<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%
Request.setcharacterencoding ("UTF-8");
%>




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.