struts解决中文乱码问题的Filter

1.在web.xml文件下配置
<filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>com.huawei.struts.MyFilter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>

写MyFilter类:
public class MyFilter implements Filter {

public void destroy() {


}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}

public void init(FilterConfig arg0) throws ServletException {

}
}

如果还是乱码就只能手工转化编码格式:
new String(request.getParameter("caseinformation").getBytes("iso8859-1"),"GBK");

你可能感兴趣的:(html,Web,xml,struts)