JS解决后台 繁体乱码 问题

脚本处理文本框内容

var name_s= document.getElementById("name_s").value; var param='name_s1='+escape(name_s); param=encodeURI(param); document.forms(0).action = "fun.getSession('PATH')/expPrepaidPhoneRecordsStatements.do?"+param; 

 后台java方法处理

String name=EncodeUtil.unescape(request.getParameter("name_s1")); 

 Java方法

/**
	 * Ajax调用时转码 在页面上使用prototype的escape()方法转码后字符串传到后台,需要用此方法转会gb2312
	 * <p>
	 * 例如:unescape(%u4E2D%u6587)会得到 "中文"
	 * </p>
	 * 
	 * @param s
	 *            转换前的源字符串
	 * @see EncodingUtil#escape(String)
	 * @return 转换后的字符串
	 */
	public static String unescape(String s) {
		if (s == null || s.equals("")) {
			return "";
		}
		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;
					// 此处也可以不处理
					// 因为页面传过来的时候ascii不会被传过来。
				} 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);
			} else { // 对应的字符未经过编码
				sbuf.append((char) ch);
			}
			i++;
		}
		return sbuf.toString();
	}

 

你可能感兴趣的:(脚本,解决繁体乱码)