一、过滤器类必须是一个servlet类,并且实现filter接口(javax.servlet)。
实现接口中的doFilter方法
public class CharEncoding extends HttpServlet implements Filter {
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
arg0.setCharacterEncoding("utf-8");
//arg1.setContentType("text/html;charset=utf-8");
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
二、在web.xml中设置刚刚创建的过滤器
<filter>
<filter-name>CharEncoding</filter-name>
<filter-class>com.cl.filter.CharEncoding</filter-class>
</filter>
<filter-mapping>
<filter-name>CharEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
三、注意jsp页面编码设为utf-8类型的