Filter_细节_过滤器拦截路径配置与Filter_细节_过滤器拦截方式配置
Filter_细节_过滤器拦截路径配置
过滤细节:
1.web.xml配置
2.过滤器的执行流程
3.过滤器的生命周期方法
4.过滤器配置详解
拦截路径配置:
1.具体资源路径:/index.jsp 只有访问indexjsp资源时候,过滤器才会被执行
.
执行:index.jsp
执行:hello.jsp
2.拦截目录: /user/* 访问/user下的所有资源部时,过滤器才会被执行
3.后缀名:*.jsp 访问所有后缀名jsp资源时,过滤器都会被执行
4.拦截所有资源:/* 访问所有资源时候,过滤器都会被执行
拦截方式配置:
5.过滤器链(配置 多个过滤器)
Filter_细节_过滤器拦截方式配置
过滤细节:
1.web.xml配置
2.过滤器的执行流程
3.过滤器的生命周期方法
4.过滤器配置详解
拦截路径配置
拦截方式配置:资源被访问的方式
注解配置:
设置dispatcherTypes属性
1.REQUEST:默认值、浏览器直接请求资源
2.FORWARD:转发访问资源
3.INCLUDE :包含访问资源
4.ERROR:错误跳转资源
5.ASYNC:异步访问资源
web.xml配置
设置<dispatcher></dispatcher>标签即可
package hf.xueqiang.filter; import javax.servlet.*; import java.io.IOException; //浏览器直接请求index.jsp资源时,该过滤器会被执行 //@WebFilter(value = "/index.jsp",dispatcherTypes = DispatcherType.REQUEST) //只有转发访问index.jsp时,该过滤器才会被执行 //@WebFilter(value = "/index.jsp",dispatcherTypes = DispatcherType.FORWARD) //浏览器直接请求index.jsp或者只有转发访问index.jsp。该过滤器才会被执行 //@WebFilter(value ="/index.jsp",dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.REQUEST}) public class FilterDemo5 implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { System.out.println("FilterDemo5****************"); chain.doFilter(request, response); } public void init(FilterConfig config) throws ServletException { } public void destroy() { } }
package hf.xueqiang.servlet; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; @WebServlet("/user/findAllServlet") public class ServletDemo1 extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("findAllServlet........."); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
package hf.xueqiang.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 request, HttpServletResponse response) throws ServletException, IOException { System.out.println("updateServlet........."); //转发到index.jsp request.getRequestDispatcher("/index.jsp").forward(request,response); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
5.过滤器链(配置 多个过滤器)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)