JavaWeb---Filter

2017-07-20  本文已影响0人  H小瑞

先上代码

public class FilterA implements Filter {

public FilterA() {

System.out.println("实例化filterA");

}

public void destroy() {

System.out.println("销毁filterA");

}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException {

System.out.println("经过FilterA的前处理");

chain.doFilter(request, response);

System.out.println("到达filterA的后处理");

}

public void init(FilterConfig fConfig) throws ServletException {

System.out.println("初始化FilterA");

}

}


public class FilterB implements Filter {

public FilterB() {

System.out.println("实例化filterB");

}

public void destroy() {

System.out.println("销毁filterB");

}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException {

System.out.println("经过FilterB的前处理");

chain.doFilter(request, response);

System.out.println("到达filterB的后处理");

}

public void init(FilterConfig fConfig) throws ServletException {

System.out.println("初始化FilterB");

}

}


public class FilterC implements Filter {

public FilterC() {

System.out.println("实例化filterC");

}

public void destroy() {

System.out.println("销毁filterC");

}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException {

System.out.println("经过FilterC的前处理");

chain.doFilter(request, response);

System.out.println("到达filterC的后处理");

}

public void init(FilterConfig fConfig) throws ServletException {

System.out.println("初始化FilterC");

}

}

web.xml的配置顺序(注意顺序):有先后顺序,而且如果同一个拦截器同时使用注解和web.xml配置会在启动项目时实例化两次

启动服务器:

实例化FilterA

初始化FilterA

实例化FilterB

初始化FilterB

实例化FilterC

初始化FilterC

浏览器访问web应用的test.jsp

经过FilterA的前处理

经过FilterB的前处理

经过FilterC的前处理

到达filterC的后处理

到达filterB的后处理

到达filterA的后处理




上一篇 下一篇

猜你喜欢

热点阅读