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 IOException, ServletException {
// 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());
}
}
<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>
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
package com.uucall.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter {
/**
* Take this filter out of service.
*/
public void destroy() {
}
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.uucall.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这个办法其实也不是很好,因为不是全局的。 decode指定UTF-8
。。之前我也试过这样- -实在找不能解决办法只好 前台用encodeURI(encodeURI("String"))转回后台decode
给个具体点的例子行吗?
首先用jquery拿到所填地方的中文值。用encodeURI(encodeURI("值"))。然后执行action。到了后台用
String tager = java.net.URLDecoder.decode("接到收的字符串temp", "utf-8");
在你的Tomcat目录下的conf文件夹里面的server.xml文件中的Connector节点加上URIEncoding="UTF-8":
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
在你的Tomcat目录下的conf文件夹里面的server.xml文件中的Connector节点加上URIEncoding="UTF-8":
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />