Filte和Interceptor的区别
interceprot
作用:拦截用户请求,对用户权限,登陆状态等信息做校验。
配置文件dispatcherServlet.xml
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/login/*.do"></mvc:mapping> <beans:bean class="org.dy.util.LoginInterceptorAdapter"></beans:bean> </mvc:interceptor> </mvc:interceptors>
拦截器TestInterceptor 通过实现HandlerInterceptorAdapter接口,实现。
package org.dy.util; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; @Controller public class LoginInterceptorAdapter extends HandlerInterceptorAdapter{ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{ System.out.println("LoginAdapter preHandle"); String login_msg = (String) request.getSession().getAttribute("login_msg"); System.out.println("login_msg: "+login_msg); if(login_msg != null) { return true; }else { response.sendRedirect("/SpringMVC_01/page/false.jsp"); return false; } } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception{ System.out.println("LoginAdapter postHandle"); } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception{ System.out.println("LoginAdapter afterCompletion"); } }
方法介绍:
preHandle方法:在请求进入controller之前执行,可以对用户登陆状态、权限先做校验。如果通过就返回true,请求到达controller,如果校验没通过就到达指定错误页面。
postHandle方法:在执行完controller之后,dispatcherservlet做视图渲染之前执行
afterCompletion方法: 是在DispatcherServlet进行视图的渲染之后执行调用,主要是进行一些资源清理等工作。
执行顺序:
preHandle
controlller
postHandle
dispatcherservlet做视图渲染
afterCompletion
Fileter
作用:设置字符编码
依赖于servlet容器,Filter的实现就是基于servlet容器(比如tomcat)
配置文件web.xml
<filter> <filter-name>testFilter</filter-name> <filter-class>org.dy.util.TestFilter</filter-class> </filter> <filter-mapping> <filter-name>testFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
过滤器TestFilter 通过实现接口 Filter实现。
package org.dy.util; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class TestFilter implements Filter { @Override public void destroy() { System.out.println("TestFilter filter destroy"); } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { System.out.println("TestFilter filter doFilter before"); arg2.doFilter(arg0, arg1); System.out.println("TestFilter filter doFilter after"); } @Override public void init(FilterConfig arg0) throws ServletException { System.out.println("TestFilter filter init"); } }
方法解释:
init方法:项目初始化时候调用
doFilter方法:过滤器过滤的指定请求到达controller之前调用FilterChain .doFilter(arg0, arg1);才会执行到controller
拦截器和过滤器执行顺序:
1、Filter.init();
2、Filter.doFilter(); before doFilter
3、HandlerInterceptor.preHandle();
4、Controller方法执行
5、HandlerInterceptor.postHandle();
6、DispatcherServlet视图渲染
7、HandlerInterceptor.afterCompletion();
8、Filter.doFilter(); after doFilter
9、Filter.destroy();
Filter和Interceptor的区别
- Filter是基于函数回调(doFilter()方法)的,而Interceptor则是基于Java反射的(AOP思想)。
- Filter依赖于Servlet容器,而Interceptor不依赖于Servlet容器。
回调函数:
比如一个Human类A方法
调用Plant类B方法,B方法调用Human类中的C方法