解决jsp页面之间,jsp和后台之间传参中文乱码问题
一、检查web.xml是否有配置编码过滤器
若没有配置请根据自己情况配置编码过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 过滤器配置信息 -->
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<!-- 过滤器设置编码文件 -->
<filter-class>
ssh.dlc.chp1.filter.SetCharacterEncodingFilter
</filter-class>
<init-param>
<!--
init-param元素定义了过滤器的初始化参数
-->
<description>给参数和值设置名称和编码类型</description>
<param-name>encode</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<!--
filter-mapping告诉容器所有与模式向匹配的请求都应该允许通过访问控制过滤器。
所有以.do结尾的访问都先通过过滤器文件过滤
-->
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
二、页面和页面之间传参乱码问题
1、对url中的中文参数进行编码
对中文参数进行加密,推荐编码两次
encodeURI(encodeURI());
```
function gotoDetail(){
var userName = $("#userName").val();
window.location.href = baseUrl + "/appinterface/homepage/otherGoods.html?userName=" + encodeURI(encodeURI(userName));
}
```
2.jsp接收
获取参数对参数解码(编码几次解码几次)
$(function(){
var sensor = ${param.userName};//测点
sensor = decodeURI(decodeURI(Request['sensor']));
alert(sensor);
});
三、页面和后台传参
1、对url中的中文参数进行编码
对中文参数进行加密,推荐编码两次
encodeURI(encodeURI());
```
function gotoDetail(){
var userName = $("#userName").val();
window.location.href = baseUrl + "/appinterface/homepage/otherGoods.html?userName=" + encodeURI(encodeURI(userName));
}
```
2.java解码
```
String userName = request.getParameter("userName");
if(!StringUtil.isEmpty(userName)){
try {
userNameUrl = URLDecoder.decode(userName,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
```