添加过滤器的配置方法

引用
方法:
1、写个类实现Filter接口
实现3个方法:

public class CharacterEncodingFilter implements Filter {

	public void destroy() {

	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) arg0;
		request.setCharacterEncoding("utf-8");
		arg2.doFilter(request, arg1);
	}

	public void init(FilterConfig arg0) throws ServletException {

	}

}

2、在web.xml里面的配置
<filter>
  		<filter-name>CharacterEncodingFilter</filter-name>
  			<filter-class>com.test.util.CharacterEncodingFilter</filter-class>
  </filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>

注意:配置的过滤器的路径名要和Servlet的配置路径要一致。

你可能感兴趣的:(xml,Web,servlet)