字符过滤器工具类

package com.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.*;
import java.io.IOException;

public class EncodingFilter implements Filter {
private Log log = LogFactory.getLog(getClass());

private String encoding = "UTF-8";

public void destroy() {
log.info("EncodingFilter destroyed.");
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
    }
public void init(FilterConfig filterConfig) throws ServletException {
String encoding = filterConfig.getInitParameter("encoding");
if (encoding != null && !"".equals(encoding.trim()))
this.encoding = encoding.trim();
log.info("Request character encoding is set to " + encoding);
}
}
posted @ 2017-10-29 20:52  me-ht  阅读(157)  评论(0编辑  收藏  举报