- 浏览: 138361 次
- 性别:
- 来自: 北京
最新评论
-
安然格思:
太久远了,文本都无法显示了
jbpm3.1中文文档 .chm -
zht110227:
这个??如此简单
onchange事件顿悟 -
xdw1626:
谢谢了,找07的
asktom 合订本集合 -
by5739:
- -这是3.0的文档吧...好像我用3.1.4...都是通过 ...
jbpm3.1中文文档 .chm -
lovefly_zero:
太强了 太感谢了
B/S开发中常用的javaScript
解决ajax 提交中文后的乱码问题:
当我门用下边的对象及方法(ajax) xh = new ActiveXObject("Microsoft.XMLHTTP") xh.onreadystatechange = getReady xh.open("POST","/test/zhongwen.jsp",false) 向服务器端传送中文数据时,zhongwen.jsp得到parameter的值就会是乱码。 那么我们怎么解决这个问题呢? 首先在页面用js的escape方法去转码 其次server端获取转码后的数据后,就用下边这个类Escape的unescape方法解码 类Escape ** * JavaScript escape/unescape 编码的 Java 实现 * author jackyz * keep this copyright info while using this method by free */ public class Escape { private final static String[] hex = { "00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F", "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F", "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F", "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F", "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F", "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F", "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F", "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F", "80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F", "90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F", "A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF", "B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF", "C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF", "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF", "E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF", "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF" }; private final static byte[] val = { 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F }; public static String escape(String s) { StringBuffer sbuf = new StringBuffer(); int len = s.length(); for (int i = 0; i < len; i++) { int ch = s.charAt(i); if (ch == ' ') { // space : map to '+' sbuf.append('+'); } else if ('A' <= ch && ch <= 'Z') { // 'A'..'Z' : as it was sbuf.append((char)ch); } else if ('a' <= ch && ch <= 'z') { // 'a'..'z' : as it was sbuf.append((char)ch); } else if ('0' <= ch && ch <= '9') { // '0'..'9' : as it was sbuf.append((char)ch); } else if (ch == '-' || ch == '_' // unreserved : as it was || ch == '.' || ch == '!' || ch == '~' || ch == '*' || ch == '\'' || ch == '(' || ch == ')') { sbuf.append((char)ch); } else if (ch <= 0x007F) { // other ASCII : map to %XX sbuf.append('%'); sbuf.append(hex[ch]); } else { // unicode : map to %uXXXX sbuf.append('%'); sbuf.append('u'); sbuf.append(hex[(ch >>> 8)]); sbuf.append(hex[(0x00FF & ch)]); } &nb
sp; } return sbuf.toString(); } public static String unescape(String s) { StringBuffer sbuf = new StringBuffer(); int i = 0; int len = s.length(); while (i < len) { int ch = s.charAt(i); if (ch == '+') { // + : map to ' ' sbuf.append(' '); } else if ('A' <= ch && ch <= 'Z') { // 'A'..'Z' : as it was sbuf.append((char)ch); } else if ('a' <= ch && ch <= 'z') { // 'a'..'z' : as it was sbuf.append((char)ch); } else if ('0' <= ch && ch <= '9') { // '0'..'9' : as it was sbuf.append((char)ch); } else if (ch == '-' || ch == '_' // unreserved : as it was || ch == '.' || ch == '!' || ch == '~' || ch == '*' || ch == '\'' || ch == '(' || ch == ')') { sbuf.append((char)ch); } else if (ch == '%') { int cint = 0; if ('u' != s.charAt(i+1)) { // %XX : map to ascii(XX) cint = (cint << 4) | val[s.charAt(i+1)]; cint = (cint << 4) | val[s.charAt(i+2)]; i+=2; } else { // %uXXXX : map to unicode(XXXX) cint = (cint << 4) | val[s.charAt(i+2)]; cint = (cint << 4) | val[s.charAt(i+3)]; cint = (cint << 4) | val[s.charAt(i+4)]; cint = (cint << 4) | val[s.charAt(i+5)]; i+=5; } sbuf.append((char)cint); } i++; } return sbuf.toString(); } public static void main(String[] args) { String stest = "中文1234 abcd[]()<+>,.~\\"; System.out.println(stest); System.out.println(escape(stest)); System.out.println(unescape(escape(stest))); } } 解码后就可以得到与页面提交的数据一致的数据了。 我们来看看为什么要采取这种方法呢? Java 的 URLEncoder.encode 方法与 JavaScript 的 unescape 方法已经不能兼容了。 现在浏览器的主流版本中 JavaScript 已经开始使用 Unicode 作为字符串的内码进行编码。即, Unicode 字符 escape 之后的样子类似于 %uABCD 而 java 的 URLEncoder.encode 方法得到的结果是 %AB%CD ,转换出来的结果必定是乱码了。 在此应该感谢写Escape类的哥们,希望此帖对大家使用ajax有所帮助。 (责任编辑:包春林)
当我门用下边的对象及方法(ajax)
xh = new ActiveXObject("Microsoft.XMLHTTP")
xh.onreadystatechange = getReady
xh.open("POST","/test/zhongwen.jsp",false)
向服务器端传送中文数据时,zhongwen.jsp得到parameter的值就会是乱码。
那么我们怎么解决这个问题呢?
首先在页面用js的escape方法去转码
其次server端获取转码后的数据后,就用下边这个类Escape的unescape方法解码
类Escape
**
* JavaScript escape/unescape 编码的 Java 实现
* author jackyz
* keep this copyright info while using this method by free
*/
public class Escape {
private final static String[] hex = {
"00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F",
"10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",
"20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",
"30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",
"40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",
"50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",
"60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F",
"70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F",
"80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F",
"90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F",
"A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF",
"B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF",
"C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF",
"D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF",
"E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF",
"F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"
};
private final static byte[] val = {
0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x3F,0x3F,0x3F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s.length();
for (int i = 0; i < len; i++) {
int ch = s.charAt(i);
if (ch == ' ') { // space : map to '+'
sbuf.append('+');
} else if ('A' <= ch && ch <= 'Z') { // 'A'..'Z' : as it was
sbuf.append((char)ch);
} else if ('a' <= ch && ch <= 'z') { // 'a'..'z' : as it was
sbuf.append((char)ch);
} else if ('0' <= ch && ch <= '9') { // '0'..'9' : as it was
sbuf.append((char)ch);
} else if (ch == '-' || ch == '_' // unreserved : as it was
|| ch == '.' || ch == '!'
|| ch == '~' || ch == '*'
|| ch == '\'' || ch == '('
|| ch == ')') {
sbuf.append((char)ch);
} else if (ch <= 0x007F) { // other ASCII : map to %XX
sbuf.append('%');
sbuf.append(hex[ch]);
} else { // unicode : map to %uXXXX
sbuf.append('%');
sbuf.append('u');
sbuf.append(hex[(ch >>> 8)]);
sbuf.append(hex[(0x00FF & ch)]);
}
&nb
sp; }
return sbuf.toString();
}
public static String unescape(String s) {
StringBuffer sbuf = new StringBuffer();
int i = 0;
int len = s.length();
while (i < len) {
int ch = s.charAt(i);
if (ch == '+') { // + : map to ' '
sbuf.append(' ');
} else if ('A' <= ch && ch <= 'Z') { // 'A'..'Z' : as it was
sbuf.append((char)ch);
} else if ('a' <= ch && ch <= 'z') { // 'a'..'z' : as it was
sbuf.append((char)ch);
} else if ('0' <= ch && ch <= '9') { // '0'..'9' : as it was
sbuf.append((char)ch);
} else if (ch == '-' || ch == '_' // unreserved : as it was
|| ch == '.' || ch == '!'
|| ch == '~' || ch == '*'
|| ch == '\'' || ch == '('
|| ch == ')') {
sbuf.append((char)ch);
} else if (ch == '%') {
int cint = 0;
if ('u' != s.charAt(i+1)) { // %XX : map to ascii(XX)
cint = (cint << 4) | val[s.charAt(i+1)];
cint = (cint << 4) | val[s.charAt(i+2)];
i+=2;
} else { // %uXXXX : map to unicode(XXXX)
cint = (cint << 4) | val[s.charAt(i+2)];
cint = (cint << 4) | val[s.charAt(i+3)];
cint = (cint << 4) | val[s.charAt(i+4)];
cint = (cint << 4) | val[s.charAt(i+5)];
i+=5;
}
sbuf.append((char)cint);
}
i++;
}
return sbuf.toString();
}
public static void main(String[] args) {
String stest = "中文1234 abcd[]()<+>,.~\\";
System.out.println(stest);
System.out.println(escape(stest));
System.out.println(unescape(escape(stest)));
}
}
解码后就可以得到与页面提交的数据一致的数据了。
我们来看看为什么要采取这种方法呢?
Java 的 URLEncoder.encode 方法与 JavaScript 的 unescape 方法已经不能兼容了。
现在浏览器的主流版本中 JavaScript 已经开始使用 Unicode 作为字符串的内码进行编码。即, Unicode 字符 escape 之后的样子类似于 %uABCD 而 java 的 URLEncoder.encode 方法得到的结果是 %AB%CD ,转换出来的结果必定是乱码了。
评论
(因为当时不叫AJAX,)
当时我说JS,不能传汉字,
结果,那时候AJAX,正是炒的时候,
没人理。
但是现在,终于有人出面解决这个问题了。
太感谢了
发表评论
-
java读取weblogic的config.xml配置信息
2009-04-28 18:54 3686当然喽,最主要的是首先要有weblogic.jar 包喽 ... -
用Java发送图文并茂的HTML邮件
2008-10-09 12:36 1506view plaincopy to clipboardprin ... -
输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
2008-08-06 09:35 1417一个比较优化的算法 import java.util.Se ... -
JasperReport、iReport
2008-08-05 21:01 2354最近工作中用到报表,而我在学习JasperReport的过程中 ... -
java中文件操作大全
2008-06-26 15:42 815一.获得控制台用户输入的信息 /** *//**获得控制台用 ... -
java数据库设计中的14个技巧
2008-06-21 09:16 813关键字: java数据库设计中的14个技巧 ... -
画饼图
2008-05-18 20:51 1339<%@ page language="java ...
相关推荐
【Java认证:Ajax中文乱码问题及解决方法】 在Java Web开发中,使用Ajax进行异步数据交互时,经常会遇到中文乱码的问题。这主要涉及到字符编码的处理,特别是当Ajax请求发送的数据包含中文字符时。以下是两种常见的...
在使用Ajax进行POST提交时,中文字符可能会遇到乱码问题,这是因为数据在传输过程中编码与解码不匹配导致的。Ajax以UTF-8编码发送数据,但服务器端可能使用了不同的字符集(如GB2312)来解析,这就产生了乱码。本文...
然而,在处理中文字符时,Ajax请求可能会遇到乱码问题,这主要是由于编码格式不一致或者处理不当导致的。本文将深入探讨Ajax中文乱码问题的成因,并提供一系列解决方案。 **一、问题原因** 1. **编码格式不一致**...
### AJAX提交中文乱码解决方法 在Web开发中,AJAX(Asynchronous JavaScript and XML)是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。它通过JavaScript发起异步请求,与服务器进行数据交换,从而...
### AJAX解决中文乱码(Java/JSP) #### 知识点概述 在Web开发中,尤其是在使用Java与JSP技术栈进行开发时,经常会遇到的一个问题是:如何处理AJAX请求中的中文字符乱码问题。本篇文章将详细介绍如何通过前端编码...
在使用MyEclipse开发工具进行Web应用开发时,尤其是采用ExtJS框架结合Ajax技术进行前后端数据交互的过程中,可能会遇到一个常见的问题:即通过Ajax发送带有中文字符的数据时,后端接收到的数据出现乱码现象。...
### AJAX传递中文参数乱码解决办法 在Web开发过程中,数据传输是不可或缺的一部分,而AJAX作为一种无需重新加载整个页面的情况下就能与服务器交换数据的技术,被广泛应用于动态数据交互场景中。然而,在处理中文等...
本文将深入探讨“ajax提交中文到servlet”的相关知识点,以解决在异步传输过程中处理中文字符的问题。 一、Ajax基础 Ajax的核心是通过JavaScript创建XMLHttpRequest对象,它允许浏览器在不重新加载整个网页的情况下...
本实例将探讨如何在Java后端和Ajax前端交互过程中解决字符编码问题,防止出现乱码。 在Java中,处理字符编码的关键在于正确设置输入流和输出流的编码。当接收到Ajax请求时,服务器需要正确识别请求中的编码,同时在...
通过使用 `encodeURI()` 函数在 AJAX 客户端编码 URL 参数和使用 `URLDecoder` 类在 Servlet 服务器端解码 URL 参数,我们可以解决 AJAX 到 Servlet 乱码问题。这样,我们的应用程序可以正确地处理中文参数,从而...
根据题目中的描述,“Ajax乱码:当调用`request.getParameter()`函数时,会自动进行一次URI的解码过程,调用时内置的解码过程会导致乱码出现。而URI 编码两次后,`request.getParameter()`函数得到的是原信息URI编码...
综上所述,解决Ajax中文乱码问题的关键在于确保客户端和服务器之间的一致性,包括请求头的设置、数据编码和解码以及页面本身的编码设置。在实际应用中,尽可能使用UTF-8作为统一编码标准,可以减少许多编码问题。...
UTF-8编码是一种常见的Unicode实现,它使用1到4个字节来表示每个Unicode字符,对于中文汉字,通常每个字占用3个字节。因此,当Ajax发送或接收UTF-8编码的数据时,理论上不应出现乱码。 然而,乱码问题可能源于以下...
在IT领域,特别是Web开发中,字符编码的处理是一大挑战,尤其是在跨浏览器、跨平台的环境下,如在Ajax请求中遇到的FireFox与IE之间的乱码兼容问题。本文将深入探讨这一问题,并提供详细的解决方案。 ### Ajax与字符...
在进行Web开发时,我们经常会遇到Ajax GET请求发送中文参数时出现乱码的问题。这个问题主要源于HTTP协议在处理非ASCII字符时的编码不一致。在本文中,我们将深入探讨这个问题,以及如何通过不同的方法来解决它。 ...
### 解决JS传递中文乱码问题 在Web开发过程中,我们经常会遇到中文字符编码的问题,尤其是在前后端数据交互时,中文字符的乱码问题更是让人头疼。本文将深入探讨如何解决JavaScript(简称JS)在传递中文字符时出现...
在JSP中,当使用Ajax进行GET请求时,可能会遇到中文乱码的问题。这通常是由于URL编码不匹配或处理不当导致的。以下是一些解决这个问题的关键知识点和步骤: 1. **URL编码问题**: URL编码是一种标准,用于将特殊...
在实际应用中,我们常常会遇到Ajax与服务器交互时出现中文乱码的问题,尤其是在发送和接收中文数据时。这个问题主要涉及到字符编码的处理,解决起来需要对HTTP协议、字符编码原理以及Struts框架的工作机制有一定的...
在Web开发中,Ajax(Asynchronous ...总结,解决Ajax乱码问题主要涉及数据编码、请求头设置和服务器端解码,而页面传值则需根据需求选择适当的方法。理解并熟练运用这些技巧,能显著提升Web应用的性能和用户体验。