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)}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值