输出文字规范过滤器:
实现Filter接口重写对应方法
public class CharacterEncodingFilter implements Filter {//导包javax.servlet.Filter @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("执行"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding("UTF-8"); servletResponse.setCharacterEncoding("UTF-8"); servletResponse.setContentType("text/html;charset=UTF-8"); filterChain.doFilter(servletRequest,servletResponse);//必须通过FilterChain调用doFilter之后过滤器才会生效 } @Override public void destroy() { System.out.println("销毁"); } }
在web.xml中配置Filter