linux系统服务器下jsp传参数乱码

在一次解决乱码问题中, 发现jsp在windows下用js原生的方法进行编码没有问题,但是到了linux下就有问题, escape,encodeURI,encodeURIComponent等都解决不了问题
但是我想了下既然原生的方法不行,我用el标签的方式对中文参数进行加密解密总该可以吧。于是用了java的java.net.URLDecoder,结果还是乱码,最后在绝望之际,用了下面的方法解决了问题

在EL表达式自定义扩展函数库中的加入
 <function>
        <description>url字符编码</description>
        <name>encodeWord</name>
        <function-class>com.miri.boss.comm.util.ELFunction</function-class>
        <function-signature> 
             java.lang.String encodeWord(java.lang.String) 
        </function-signature> 
        <example>${f:encodeWord('要编码的字符串')}</example>
    </function>
    <function>
        <description>url字符解码</description>
        <name>decodeWord</name>
        <function-class>com.miri.boss.comm.util.ELFunction</function-class>
        <function-signature> 
             java.lang.String decodeWord(java.lang.String) 
        </function-signature> 
        <example>${f:decodeWord('要解码的字符串')}</example>
    </function>


ELFunction.java中写加解密方法
/**
	 * 对字符串escape编码
	 * 
	 * @param word
	 * @return
	 */
	public static String encodeWord(String word)
	{
		int i;
		char j;
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(word.length() * 6);
		for (i = 0; i < word.length(); i++)
		{
			j = word.charAt(i);
			if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j))
				tmp.append(j);
			else if (j < 256)
			{
				tmp.append("%");
				if (j < 16)
					tmp.append("0");
				tmp.append(Integer.toString(j, 16));
			}
			else
			{
				tmp.append("%u");
				tmp.append(Integer.toString(j, 16));
			}
		}
		return tmp.toString();
	}
	
	/**
	 * 对escape码进行unescape解码
	 * 
	 * @param word
	 * @return
	 */
	public static String decodeWord(String word)
	{
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(word.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < word.length())
		{
			pos = word.indexOf("%", lastPos);
			if (pos == lastPos)
			{
				if (word.charAt(pos + 1) == 'u')
				{
					ch = (char) Integer.parseInt(word.substring(pos + 2, pos + 6), 16);
					tmp.append(ch);
					lastPos = pos + 6;
				}
				else
				{
					ch = (char) Integer.parseInt(word.substring(pos + 1, pos + 3), 16);
					tmp.append(ch);
					lastPos = pos + 3;
				}
			}
			else
			{
				if (pos == -1)
				{
					tmp.append(word.substring(lastPos));
					lastPos = word.length();
				}
				else
				{
					tmp.append(word.substring(lastPos, pos));
					lastPos = pos;
				}
			}
		}
		return tmp.toString();
	}

在jsp页面中引入
<%@ taglib prefix="ks" uri="http://www.miri.com/elfunction" %>

页面中对中文参数的加密(两次加密)
${ks:encodeWord(ks:encodeWord(programList.channelName))}

页面中对中文参数的解密
${ks:decodeWord(param.channelName)}

你可能感兴趣的:(java,jsp,linux,windows,xml)