解决jsp页面之间,jsp和后台之间传参中文乱码问题

解决jsp页面之间,jsp和后台之间传参中文乱码问题

一、检查web.xml是否有配置编码过滤器


若没有配置请根据自己情况配置编码过滤器

  
<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>SetCharacterEncodingFilterfilter-name>  
    
  <filter-class>  
   ssh.dlc.chp1.filter.SetCharacterEncodingFilter  
  filter-class>  
  <init-param>  
        
   <description>给参数和值设置名称和编码类型description>  
   <param-name>encodeparam-name>  
   <param-value>GBKparam-value>  
  init-param>  
 filter>  
 <filter-mapping>  
    
  <filter-name>SetCharacterEncodingFilterfilter-name>  
  <url-pattern>*.dourl-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();
        }
    }
```

你可能感兴趣的:(页面传参)