地址栏传递中文参数乱码在js里用escape转码


Posted in Javascript onAugust 28, 2013

乱码,乱码,头大,烦,话说设置Tomcat后可以解决,但有的时候不管用了,这时,我们可以考虑转码方式不直接传中文,在js里用escape转码,记得需要用两次,如:

var str=escape("这是中文参数");

str=escape(str);

xxx.do?name=str;

在后台通过一个类的方法再转回来:

Escape.unescape(request.getParameter("name"));

Escape类如下:

package com.laneasy.hy.util; /** 
* js编转码java实现类. 
* 
* @author yangjinde 
*/ 
public class Escape { 
/** The Constant hex. */ 
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" }; 
/** The Constant val. */ 
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 }; 
/** 
* Escape. 
* 
* @param s 
* the s 
* 
* @return the string 
*/ 
/** 
* 编码 
* 
* @param s 
* @return 
*/ 
public static String escape(String s) { 
if (s != null) { 
StringBuffer sbuf = new StringBuffer(); 
int len = s.length(); 
for (int i = 0; i < len; i++) { 
int ch = s.charAt(i); 
if ('A' <= ch && ch <= 'Z') { 
sbuf.append((char) ch); 
} else if ('a' <= ch && ch <= 'z') { 
sbuf.append((char) ch); 
} else if ('0' <= ch && ch <= '9') { 
sbuf.append((char) ch); 
} else if (ch == '-' || ch == '_' || ch == '.' || ch == '!' 
|| ch == '~' || ch == '*' || ch == '\'' || ch == '(' 
|| ch == ')') { 
sbuf.append((char) ch); 
} else if (ch <= 0x007F) { 
sbuf.append('%'); 
sbuf.append(hex[ch]); 
} else { 
sbuf.append('%'); 
sbuf.append('u'); 
sbuf.append(hex[(ch >>> 8)]); 
sbuf.append(hex[(0x00FF & ch)]); 
} 
} 
return sbuf.toString(); 
} 
return null; 
} 
/** 
* Unescape. 
* 
* @param s 
* the s 
* 
* @return the string 
*/ 
/** 
* 解码 说明:本方法保证 不论参数s是否经过escape()编码,均能得到正确的“解码”结果 
* 
* @param s 
* @return 
*/ 
public static String unescape(String s) { 
if (s != null) { 
StringBuffer sbuf = new StringBuffer(); 
int i = 0; 
int len = s.length(); 
while (i < len) { 
int ch = s.charAt(i); 
if ('A' <= ch && ch <= 'Z') { 
sbuf.append((char) ch); 
} else if ('a' <= ch && ch <= 'z') { 
sbuf.append((char) ch); 
} else if ('0' <= ch && ch <= '9') { 
sbuf.append((char) ch); 
} else if (ch == '-' || ch == '_' || ch == '.' || ch == '!' 
|| ch == '~' || ch == '*' || ch == '\'' || ch == '(' 
|| ch == ')') { 
sbuf.append((char) ch); 
} else if (ch == '%') { 
int cint = 0; 
if ('u' != s.charAt(i + 1)) { 
cint = (cint << 4) | val[s.charAt(i + 1)]; 
cint = (cint << 4) | val[s.charAt(i + 2)]; 
i += 2; 
} else { 
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); 
} else { 
sbuf.append((char) ch); 
} 
i++; 
} 
return sbuf.toString(); 
} 
return null; 
} 
}
Javascript 相关文章推荐
js解析json读取List中的实体对象示例
Mar 11 Javascript
js函数内变量的作用域分析
Jan 12 Javascript
jQuery实现HTML表格单元格的合并功能
Apr 06 Javascript
js中使用使用原型(prototype)定义方法的好处详解
Jul 04 Javascript
Bootstrap表单简单实现代码
Mar 06 Javascript
简单实现JavaScript弹幕效果
Aug 27 Javascript
用vue封装插件并发布到npm的方法步骤
Oct 18 Javascript
Node.js 中使用 async 函数的方法
Nov 20 Javascript
vue的token刷新处理的方法
Jul 17 Javascript
Vue+Element UI+vue-quill-editor富文本编辑器及插入图片自定义
Aug 20 Javascript
node crawler如何添加promise支持
Feb 01 Javascript
Vue中实现回车键切换焦点的方法
Feb 19 Javascript
在JavaScript中实现类的方式探讨
Aug 28 #Javascript
实用的Jquery选项卡TAB示例代码
Aug 28 #Javascript
jQuery实现图片信息的浮动显示实例代码
Aug 28 #Javascript
JQuery筛选器全系列介绍
Aug 27 #Javascript
HTML Color Picker(js拾色器效果)
Aug 27 #Javascript
Js中的onblur和onfocus事件应用介绍
Aug 27 #Javascript
javascript:void(0)使用探讨
Aug 27 #Javascript
You might like
php INI配置文件的解析实现分析
2011/01/04 PHP
给WordPress中的留言加上楼层号的PHP代码实例
2015/12/14 PHP
Extjs中的GridPanel隐藏列会显示在menuDisabled中解决方法
2013/01/27 Javascript
5款JavaScript代码压缩工具推荐
2014/07/07 Javascript
PHP结合jQuery实现红蓝投票功能特效
2015/07/22 Javascript
vue.js通过自定义指令实现数据拉取更新的实现方法
2016/10/18 Javascript
BootStrap中
2016/12/10 Javascript
关于vue-router路径计算问题
2017/05/10 Javascript
浅谈vuex之mutation和action的基本使用
2017/08/29 Javascript
JQuery 选择器、DOM节点操作练习实例
2017/09/28 jQuery
解决vue-cli3 使用子目录部署问题
2018/07/19 Javascript
vue实现多条件和模糊搜索功能
2019/05/28 Javascript
VUE写一个简单的表格实例
2019/08/06 Javascript
vue中nextTick用法实例
2019/09/11 Javascript
vue实现表格过滤功能
2019/09/27 Javascript
利用PHP实现递归删除链表元素的方法示例
2020/10/23 Javascript
Python三元运算实现方法
2015/01/12 Python
Linux下用Python脚本监控目录变化代码分享
2015/05/21 Python
使用Python对Excel进行读写操作
2017/03/30 Python
Python3实现发送QQ邮件功能(html)
2017/12/15 Python
python 编码规范整理
2018/05/05 Python
python3 拼接字符串的7种方法
2018/09/12 Python
python实现比较类的两个instance(对象)是否相等的方法分析
2019/06/26 Python
python-OpenCV 实现将数组转换成灰度图和彩图
2020/01/09 Python
python Matplotlib数据可视化(2):详解三大容器对象与常用设置
2020/09/30 Python
静态成员和非静态成员的区别
2012/05/12 面试题
教师对学生的寄语
2014/04/03 职场文书
学校食品安全责任书
2015/01/29 职场文书
成品仓管员岗位职责
2015/04/01 职场文书
2015年主婚人婚礼致辞
2015/07/28 职场文书
2015年度个人工作总结报告
2015/10/24 职场文书
护理自荐信
2019/05/14 职场文书
Flask使用SQLAlchemy实现持久化数据
2021/07/16 Python
九大龙王魂骨,山龙王留下躯干骨,榜首死的最憋屈(被捏碎)
2022/03/18 国漫
Spring Boot DevTools 全局配置学习指南
2022/03/31 Java/Android
Python内置的数据类型及使用方法
2022/04/13 Python