日期:2014-05-17 浏览次数:20646 次
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>