在Filter中拦截路径配置有四种形式
具体资源路径拦截:@WebFilter("/index.jsp") //这是指访问index.jsp的时候会经过过滤器
具体目录拦截:@WebFilter("/user/*") //这是指访问user目录下的所有资源时会经过过滤器
具体后缀名拦截:@WebFilter("*.jsp") //这时指访问后缀名为.jsp的资源时会经过过滤器
所有资源拦截:@WebFilter("/*") //这是指访问所有资源的时候都会经过过滤器
首先演示第一种,具体资源路径拦截,如果访问的不是index.jsp,则不会执行过滤器
package com.zhiying.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/index.jsp")
public class FilterDemo4 implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("执行了过滤器");
chain.doFilter(request,response);
}
}
下面是我访问了hello.jsp,就没有执行过滤器
第二种是具体目录拦截,这里演示,是通过两个servlet例子,我们去访问servlet,具体演示如下,首先我们写两个servlet
package com.zhiying.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/user/a1")
public class ServletDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("servlet1...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
package com.zhiying.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/user/a2")
public class ServletDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("servlet2...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
然后修改我们的Filter
package com.zhiying.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/user/*")
//@WebFilter("/index.jsp")
public class FilterDemo4 implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("执行了过滤器");
chain.doFilter(request,response);
}
}
当我们访问/user/a1和a2的时候,发现过滤器被执行了,而我们访问index.jsp的时候过滤器没有别执行
第三种是后缀名拦截,就是所有该后缀名的资源都会执行过滤器,这里为了演示,我又创建了一个html文件,这里给出html文件和修改该过的Java类
Title
html...
package com.zhiying.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("*.jsp")
// @WebFilter("/user/*")
// @WebFilter("/index.jsp")
public class FilterDemo4 implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("执行了过滤器");
chain.doFilter(request,response);
}
}
从下图可以看出,我们的jsp文件都会被经过过滤器
下面是访问html资源,我们会发现,没有经过过滤器,空空如也
最后一种就是拦截所有资源,仍然给出Java类
package com.zhiying.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/*")
// @WebFilter("*.jsp")
// @WebFilter("/user/*")
// @WebFilter("/index.jsp")
public class FilterDemo4 implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("执行了过滤器");
chain.doFilter(request,response);
}
}
不管是执行index.jsp还是hello.jsp还是html资源,都会经过过滤器