filter 过滤器简单应用

(1)写一个实现Filter接口的类,该类也即是一个Filter

public class TestFilter extends HttpServlet implements Filter {

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		filterChain.doFilter(request, response);

	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

	/**
	 * Constructor of the object.
	 */
	public TestFilter() {
		super();
	}

}

 

(2)在web.xml 中配置

 <filter>
	<filter-name>TestFilter</filter-name>
    <filter-class>com.ambow.filter.TestFilter</filter-class>
  </filter>
<filter-mapping>
    <filter-name>TestFilter</filter-name>
     <!--  对 index.jsp 请求前执行拦截过滤 -->
    <url-pattern>/index.jsp</url-pattern>
   </filter-mapping>

 

 

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