Filter的介绍
拦截路径配置
过滤器的拦截路径一般有这4种方式
- 拦截所有资源:/*,访问所有的资源,过滤器都会被执行
- 具体资源路径:/myServlet/demo1,只有访问myServlet/demo1资源时,过滤器才会被执行
- 拦截目录:/myServlet/*,访问/myServlet下所有资源是,过滤器都会被执行
- 后缀名被拦截:*.jsp,访问后缀名为jsp资源时,过滤器都会被执行
下面我们就举例演示这4种方式
来2个Servlet,资源路径分别取为/myServlet/demo1和/myServlet/demo2,一个jsp页面,myJSP.jsp,一个过滤器demo1
/myServlet/demo1代码
package com.lingaolu.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
/**
* @author 林高禄
* @create 2020-08-04-19:34
*/
@WebServlet("/myServlet/demo1")
public class Demo1 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("/myServlet/demo1执行了......");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
/myServlet/de