例如:
url="searchPeoBySomeWords.do?KeyWord="+escape(escape(obj.value))。
这样“包装”后我们在action中解码,用Escape.java中的unescape静态方法就可解码。
String keyWord = Escape.unescape(request.getParameter("KeyWord")).trim();
这样就可以了。
/**
* 将url中文参数编码或者反编码
* 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,0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3F,0x3F,0x3F,0x3F,0x3F,0x3F,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)]);
}
}
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)));
// }
}
分享到:
相关推荐
"在页面上用action传递参数到后台出现乱码的解决方法"这一主题聚焦于解决一个常见的问题,即当通过ACTION(比如一个特定的后台处理方法)接收前端发送的参数时,可能出现的字符编码不匹配导致的乱码现象。...
如果直接拼接,传到后台Action的参数对象中后取出会是乱码,需要编码后再拼接到URL上。 解决方法是在Action中添加一个成员变量,保存编码后的中文参数。在vm页面渲染时取出这个变量值,再拼接超链接。 在这里碰到的...
综上所述,解决JSP中的中文乱码问题需要从多个方面入手,既要确保服务器端和客户端的数据交互过程中的编码一致性,又要保证各个组件间的编码设置协调一致。只要按照以上介绍的方法逐一排查和调整,便可以有效地解决...
总之,动态表单提供了一种有效解决URL中文乱码问题的策略,特别是在需要传递复杂或包含中文的参数时,这种方法显得尤为实用。在实际开发中,我们可以根据项目需求和具体情况选择最适合的解决方案。
在Web应用程序中,用户通过表单提交的数据经常需要传递到后台进行处理,如果表单数据中包含中文字符,则容易出现乱码问题。 **解决方法**: 1. **修改Tomcat配置**:在Tomcat的`conf/server.xml`文件中,找到`...
在IT领域,特别是Web开发中,字符编码的处理是一大挑战,尤其是在跨浏览器、跨平台的环境下,如在Ajax请求中遇到的FireFox与IE之间的乱码兼容问题。本文将深入探讨这一问题,并提供详细的解决方案。 ### Ajax与字符...
本文将详细讲解如何解决STRUTS+AJAX+JSP请求到后台的乱码问题。 首先,我们来看一个关键的JavaScript代码片段,这是解决前端AJAX请求时乱码问题的关键: ```javascript var fileName = document.getElementById(...
在后台Action中,接收到URL参数后,需要使用相应的解码方法将其还原。在Java中,可以使用`java.net.URLDecoder`类的`decode()`方法来解码URL参数。例如: ```java String cNum = contract.getContractNum(); ...
在使用jQuery的ajax()函数进行数据传输时,中文乱码问题常常困扰着开发者。这个问题主要出现在数据发送到服务器时,由于编码格式不一致导致的字符乱码。以下是一些解决此问题的方法: 方法一:使用`encodeURI()`...
// 这里的msg可以在后台Action配置,或者在这里直接写提示信息 }, 'text'); ``` 在这个例子中,`data`参数是一个对象,包含了要传递给后台的键值对。`function(data)`是回调函数,当后台返回数据时被调用。`'text'...
这里需要注意的是,后台需要设置正确的字符编码,以防止中文乱码问题,并且设置响应类型为JSON,以便前端解析。 在ExtJS与ASP.NET或其他后台框架交互时,基本思路是一致的,只是后台处理请求的方式会根据所使用的...
以上代码会向服务器发送一个GET请求,URL为`test.action`,并携带参数`testVar`,值为`someValue`。当服务器返回JSON数据后,回调函数会接收到这些数据,并可以进一步处理,如在上述例子中,我们打印了返回的`test...
在从前台JS传到后台Action的中文乱码问题上,可以通过以下方式解决: - 使用`new String(string.getBytes("ISO-8859-1"), "UTF-8")`进行解码,将ISO-8859-1编码的字节流转换为UTF-8编码的字符串。 - 使用`java.net...
在后台Action中,我们需要将查询到的用户信息转换为JSON对象并返回,确保数据类型与前端请求的数据类型匹配。 ```csharp public void initAll(ModelMap model, HttpServletRequest request, HttpServletResponse ...
在Java Server Pages (JSP) 技术中,页面间传递参数是一个常见的需求。以下是四种在JSP中传递参数的方法: ...在实际开发中,开发者应根据具体需求选择合适的参数传递方法,并注意处理好中文乱码问题。
总结:通过以上步骤,我们可以实现一个基于jQuery的下拉框自动提示功能,同时解决了前后台中文乱码问题。对于不同的后端框架,如Spring MVC或Struts2,只需要将Servlet替换为相应的Controller或Action,保持相同的...
application/x-www-form-urlencoded是默认的编码格式,当action为get时,浏览器会将form数据转换成一个字串(name1=value1&name2=value2...),然后把这个字串append到url后面,用?分割,加载这个新的url。当action...
27. **如何解决页面乱码问题?** - 对于JSP页面,可以通过设置Page指令的charset属性解决;对于表单提交,可以在Servlet中设置字符编码,或者使用过滤器统一处理请求和响应的编码问题。 以上这些问题涵盖了软件...