过滤器拦截路径配置
1.具体资源路径:/index.jsp 只有访问index.jsp资源时,过滤器才会被执行
2.拦截目录:/user/* 访问/user下的所有资源时,过滤器都会被执行
3.后缀名拦截:*.jsp 访问所有jsp资源时,过滤器都会被执行
4.拦截所有资源:/* 访问所有资源时,过滤器都会被执行
package com.example.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /* 过滤器快速入门程序 */ //@WebFilter("/index.jsp")// 1.具体资源路径:/index.jsp 只有访问index.jsp资源时,过滤器才会被执行 //@WebFilter("/user/findAllServlet")// 2.拦截目录:/user/* 访问/user下的所有资源时,过滤器都会被执行 @WebFilter("*.jsp")// 3.后缀名拦截:*.jsp 访问所有jsp资源时,过滤器都会被执行 //@WebFilter("/*")// 4.访问所有资源之前,都会执行该过滤器 public class FilterDemo4 implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { System.out.println("filterDemo4..."); //放行 chain.doFilter(req, resp); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }
过滤器拦截方式配置
拦截方式配置:资源被访问的方式
注解配置:
设置dispatcherTypes属性
1.REQUEST:默认值,浏览器直接请求资源
2.FORWARD:转发访问资源
3.INCLUDE:包含访问资源
4.ERROR:错误跳转资源
5.ASYNC:异步访问资源
package com.example.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; 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 { @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { System.out.println("filterDemo5..."); //放行 chain.doFilter(req, resp); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }
servlet:
package com.example.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(name = "updateServlet", value = "/user/updateServlet") public class ServletDemo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("updateServlet..."); //转发 request.getRequestDispatcher("/index.jsp").forward(request, response); } }
web.xml配置
设置<dispatcher></dispatcher>标签即可
<dispatcher>REQUEST</dispatcher>
过滤器链(配置多个过滤器)
执行顺序:如果有两个过滤器:过滤器1 和过滤器2
1.过滤器1
2.过滤器2
3.资源执行
4.过滤器2
5.过滤器1
package com.example.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter("/*") public class FilterDemo6 implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { System.out.println("filterDemo6执行了..."); //放行 chain.doFilter(req, resp); System.out.println("FilterDemo6回来了..."); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }
package com.example.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter("/*") public class FilterDemo7 implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { System.out.println("filterDemo7执行了..."); //放行 chain.doFilter(req, resp); System.out.println("FilterDemo7回来了..."); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }
过滤器先后顺序问题:
1.注解配置:按照类名的字符串比较规则,值小的先执行
如:AFilter 和 BFilter AFliter就先执行了(按类名大小来执行顺序的)
2.web.xml配置:<filter-mapping>谁定义在上边,谁先执行
<filter> <filter-name>filedemo1</filter-name> <filter-class>com.tomcat1.web.filter.FilterDemo7</filter-class> </filter> <filter-mapping> <filter-name>filedemo1</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>filedemo2</filter-name> <filter-class>com.tomcat1.web.filter.FilterDemo6</filter-class> </filter> <filter-mapping> <filter-name>filedemo2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
标签:void,配置,jsp,过滤器,import,拦截,servlet,public From: https://www.cnblogs.com/qihaokuan/p/17072799.html