Fitter-细节-过滤器拦截路径配置
1. 具体资源路径: /index.jsp 只有访问index.jsp资源时,过滤器才会被执行
2. 拦截目录: /user/* 访问/user下的所有资源时,过滤器都会被执行
3. 后缀名拦截: *.jsp 访问所有后缀名为jsp资源时,过滤器都会被执行
4. 拦截所有资源:/* 访问所有资源时,过滤器都会被执行
1. 具体资源路径: /index.jsp 只有访问index.jsp资源时,过滤器才会被执行
创建hello.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html> <head> <title>JSP - Hello World</title> </head> <body> hello.jsp </body> </html>
创建过滤器
package com.example.day_19_fitterlistener.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * 过滤器快速入门 */ @WebFilter("/index.jsp")//1. 具体资源路径: /index.jsp 只有访问index.jsp资源时,过滤器才会被执行 public class FilterDemo4 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { Filter.super.init(filterConfig); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("FitterDemo4......"); } @Override public void destroy() { Filter.super.destroy(); } }
我们运行代码,只有访问index.jsp的时候控制台才会输出FitterDemo4.....,访问hello.jsp的时候,控制台不会输出
2. 拦截目录: /user/* 访问/user下的所有资源时,过滤器都会被执行
创建FitterDemo4
package com.example.day_19_fitterlistener.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * 过滤器快速入门 */ //@WebFilter("/index.jsp")//1. 具体资源路径: /index.jsp 只有访问index.jsp资源时,过滤器才会被执行 @WebFilter("/user/*")//2. 拦截目录: /user/* 访问/user下的所有资源时,过滤器都会被执行 public class FilterDemo4 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { Filter.super.init(filterConfig); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("FitterDemo4......"); } @Override public void destroy() { Filter.super.destroy(); } }
创建两个ServletDemo
package com.example.day_19_fitterlistener.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/findAllServlet") public class ServletDemo1 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("findAllServlet..."); super.doPost(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
package com.example.day_19_fitterlistener.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/updateServlet") public class ServletDemo2 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("updateServlet..."); super.doPost(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
3. 后缀名拦截: *.jsp 访问所有后缀名为jsp资源时,过滤器都会被执行
package com.example.day_19_fitterlistener.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * 过滤器快速入门 */ //@WebFilter("/index.jsp")//1. 具体资源路径: /index.jsp 只有访问index.jsp资源时,过滤器才会被执行 //@WebFilter("/user/*")//2. 拦截目录: /user/* 访问/user下的所有资源时,过滤器都会被执行 @WebFilter("*.jsp")//3. 后缀名拦截: *.jsp 访问所有后缀名为jsp资源时,过滤器都会被执行 public class FilterDemo4 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { Filter.super.init(filterConfig); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("FitterDemo4......"); } @Override public void destroy() { Filter.super.destroy(); } }
只有访问后缀名为jsp的才会输出
最后一种,咱们之前已经展示过, 在这里就不展示了
标签:index,Fitter,jsp,过滤器,import,拦截,servlet,javax From: https://www.cnblogs.com/yuzong/p/17074909.html