过滤器
public class MyFilter extends HttpServlet implements Filter {
@Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
arg0.setCharacterEncoding("utf-8"); //设置接收编码
arg1.setContentType("text/html;charset=utf-8");
arg2.doFilter(arg0, arg1); //必须
}
Web.XML
<!-- 过滤器 -->
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>cn.yct.servlet.MyFilter</filter-class> 包名
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>