乱码有很多中原因
下面是其中一种解决办法,你可以试试
修改tomcat的conf/server.xml
把
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
改成
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" [color=red]URIEncoding="UTF-8"/[/color]>
如果还乱码,请在web.xml中添加一个过滤器
下面是其中一种解决办法,你可以试试
修改tomcat的conf/server.xml
把
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
改成
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" [color=red]URIEncoding="UTF-8"/[/color]>
如果还乱码,请在web.xml中添加一个过滤器
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.worthtech.app.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
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 CharacterEncodingFilter implements Filter {
private String encoding=null;
private boolean ignore=true;
public void destroy() {
// TODO Auto-generated method stub
encoding=null;
}
/**
* 过滤请求,用户提交请求时起作用
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
if(ignore||request.getCharacterEncoding()==null){
String encoding=getEncoding();
if(encoding!=null){
request.setCharacterEncoding(encoding);
}
}
// System.out.println("encoding===="+encoding);
response.setContentType("text/html;charset="+encoding);
// do something here
//当前工作全部完成后,将请求放开给过滤器链的下一filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
this.encoding=filterConfig.getInitParameter("encoding");
String value=filterConfig.getInitParameter("ignore");
if(value==null){
this.ignore=true;
}else if(value.equalsIgnoreCase("true")){
this.ignore=true;
}else if(value.equalsIgnoreCase("yes")){
this.ignore=true;
}else{
this.ignore=false;
}
}
protected String getEncoding(){
return this.encoding;
}
}