web.xml:
<filter>
<filter-name>FilterTest</filter-name>
<filter-class>org.util.filterTest</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterTest</filter-name>
<url-pattern>/h1.jsp</url-pattern> //指定过滤器过滤的页面,如果写“/”则是所有请求
</filter-mapping>
filterTest.java:
public class filterTest implements Filter{
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("filter start");
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
在游览器中输入http://localhost:8080/Test/h1.jsp则会打印filter start