日期:2014-05-17  浏览次数:20646 次

jsp向action传值中文乱码,求帮助
数据库是mysql,字符集是utf-8的,如图
jsp页面也是utf-8的字符集,传值到action中,中文乱码,如果用了String title1 = new String(title.getBytes("ISO-8859-1"),"UTF-8");转码能得到正常中文。想加个过滤器,过滤器文件如下
public class EncodingFilter implements Filter {
private String characterEncoding;
private boolean enabled;
@Override
public void destroy() {
// TODO Auto-generated method stub
characterEncoding = null;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOExceptionServletException {
// TODO Auto-generated method stub
if (enabled || characterEncoding!=null) {
//System.out.println("characterEncoding===="+characterEncoding);
request.setCharacterEncoding(characterEncoding);
response.setCharacterEncoding(characterEncoding);
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
characterEncoding = filterConfig.getInitParameter("characterEncoding");
enabled = "true".equalsIgnoreCase(filterConfig.getInitParameter("enabled").trim());
}
}

web.xml如下
<filter>
  <filter-name>EncodingFilter</filter-name>
  <filter-class>common.util.EncodingFilter</filter-class>
   <init-param>
            <param-name>characterEncoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>enabled</param-name>
            <param-value>false</param-value>
        </init-param>
 </filter>

 <filter-mapping>
  <filter-name>EncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

但是得到的中文依然是乱码,求大神帮助

------解决方案--------------------
。。之前我也试过这样- -实在找不能解