package com.util;
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 CharEncodingFilter implements Filter
{
private String charSet;
public void init(FilterConfig config) throws ServletException
{
charSet = config.getInitParameter("charset");
if (charSet == null && charSet.length() < 1)
{
charSet = "UTF-8";
}
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
// 设定字符集
request.setCharacterEncoding(this.getCharSet());
response.setContentType("text/html;charset=UTF-8");
chain.doFilter(request, response);
}
public void destroy()
{
this.setCharSet(null);
}
public void setCharSet(String charSet)
{
this.charSet = charSet;
}
public String getCharSet()
{
return (this.charSet);
}
}