解决中文乱码

页面:pageEncoding="utf-8"

传递时出现乱码
1 在 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" URIEncoding="UTF-8"/>

2 添加过滤器 CharacterEncodingFilte
package com.chen.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 CharacterEncodingFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
                response.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
}

}

3修改web.xml文件: 加入
  <filter>
  <filter-name>characterEncodin</filter-name>
  <filter-class>com.chen.filter.CharacterEncodingFilter</filter-class>
  </filter>
 
  <filter-mapping>
  <filter-name>characterEncodin</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>



你可能感兴趣的:(tomcat,Web,xml,servlet)