1.问题:如何使过滤器拦截转发的请求和响应?
- Filter 过滤器默认拦截的是客户端发送过来的请求,但是在实际开发中,我们还有请求转发,以及由服务器触发调用的全局错误页面。默认情况下过滤器是不参与过滤的,要想使用,就需要我们配置。
2.拦截方式
<filter>
<filter-name>filterDemo05</filter-name>
<filter-class>com.itheima.filter.FilterDemo05</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>filterDemo05</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>ERROR</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
核心:知道REQUEST和FORWARD就行了,REQUEST表示拦截客户端浏览器发送的请求,FORWARD拦截转发的请求。 如果是使用注解配置过滤器,那么拦截方式设置如下:@WebFilter(value = {"/servletDemo01","/servletDemo02"},dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.REQUEST})