1、拦截器的使用

1)拦截器接口

实现了Spring框架的HandlerInterceptor接口,HandlerInterceptor采用了JDK8的接口定义,可以使得不需要每个方法都去实现

2)方法的调用顺序

图中的处理器方法是指@Controller
image.png

3)Java代码

  1. public class MyInterceptor implements HandlerInterceptor {
  2. @Override
  3. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  4. System.out.println("调用preHandle方法"+handler);
  5. String loginName = (String)request.getSession().getAttribute("loginName");
  6. if (loginName == null || !"高云".equals(loginName)){
  7. request.getSession().setAttribute("error","没有登录!");
  8. request.getRequestDispatcher("error.jsp").forward(request,response);
  9. return false;
  10. }
  11. String name = request.getParameter("name");
  12. Integer age = Integer.parseInt(request.getParameter("age"));
  13. if (name != null ){
  14. //张三不能登录
  15. if ("zs".equals(name)){
  16. request.setAttribute("error","张三不能登录!");
  17. request.getRequestDispatcher("error.jsp").forward(request,response);
  18. return false;
  19. }
  20. }
  21. return true;
  22. }
  23. @Override
  24. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  25. System.out.println("调用postHandle方法,这个方法是在Controller方法执行之后,执行");
  26. }
  27. @Override
  28. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  29. System.out.println("调用afterCompletion方法,该方法在请求处理完成后,做最后的处理,可以用来清理缓存什么的");
  30. }
  31. }

4)dispatcherServlet.xml文件配置

  1. <mvc:interceptors>
  2. <mvc:interceptor>
  3. <!--拦截请求地址-->
  4. <mvc:mapping path="/showMessage.do"/>
  5. <!--拦截器类所在包-->
  6. <bean class="com.bjpowernode.interceptor.MyInterceptor"/>
  7. </mvc:interceptor>
  8. </mvc:interceptors>

2、多个拦截器的使用规则

1)执行顺序

image.png

2)dispatcherServlet.xml配置文件

  1. <mvc:interceptors>
  2. <mvc:interceptor>
  3. <!--拦截请求地址-->
  4. <mvc:mapping path="/showMessage.do"/>
  5. <!--拦截器类所在类-->
  6. <bean class="com.bjpowernode.interceptor.MyInterceptor"/>
  7. </mvc:interceptor>
  8. <mvc:interceptor>
  9. <!--拦截请求地址2-->
  10. <mvc:mapping path="/showMessage.do"/>
  11. <!--拦截器类所在类2-->
  12. <bean class="com.bjpowernode.interceptor.MyInterceptor2"/>
  13. </mvc:interceptor>
  14. </mvc:interceptors>

3)类以及输出结果

  1. public class MyInterceptor implements HandlerInterceptor {
  2. @Override
  3. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  4. System.out.println("1111111111调用preHandle方法"+handler);
  5. String loginName = (String)request.getSession().getAttribute("loginName");
  6. if (loginName == null || !"高云".equals(loginName)){
  7. request.getSession().setAttribute("error","没有登录!");
  8. request.getRequestDispatcher("error.jsp").forward(request,response);
  9. return false;
  10. }
  11. String name = request.getParameter("name");
  12. Integer age = Integer.parseInt(request.getParameter("age"));
  13. if (name != null ){
  14. //张三不能登录
  15. if ("zs".equals(name)){
  16. request.setAttribute("error","张三不能登录!");
  17. request.getRequestDispatcher("error.jsp").forward(request,response);
  18. return false;
  19. }
  20. }
  21. return true;
  22. }
  23. @Override
  24. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  25. System.out.println("11111111调用postHandle方法,这个方法是在Controller方法执行之后,执行");
  26. }
  27. @Override
  28. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  29. System.out.println("111111111调用afterCompletion方法,该方法在请求处理完成后,做最后的处理,可以用来清理缓存什么的");
  30. }
  31. }
  32. ***************************************************************************************************************************************************
  33. public class MyInterceptor2 implements HandlerInterceptor {
  34. @Override
  35. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  36. System.out.println("22222222222调用preHandle方法"+handler);
  37. String loginName = (String)request.getSession().getAttribute("loginName");
  38. if (loginName == null || !"高云".equals(loginName)){
  39. request.getSession().setAttribute("error","没有登录!");
  40. request.getRequestDispatcher("error.jsp").forward(request,response);
  41. return false;
  42. }
  43. String name = request.getParameter("name");
  44. Integer age = Integer.parseInt(request.getParameter("age"));
  45. if (name != null ){
  46. //张三不能登录
  47. if ("zs".equals(name)){
  48. request.setAttribute("error","张三不能登录!");
  49. request.getRequestDispatcher("error.jsp").forward(request,response);
  50. return false;
  51. }
  52. }
  53. return true;
  54. }
  55. @Override
  56. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  57. System.out.println("222222222222调用postHandle方法,这个方法是在Controller方法执行之后,执行");
  58. }
  59. @Override
  60. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  61. System.out.println("22222222222调用afterCompletion方法,该方法在请求处理完成后,做最后的处理,可以用来清理缓存什么的");
  62. }
  63. }

4)输出结果

image.png

3、拦截器和过滤器的区别

1)过滤器是servlet中的对象, 拦截器是框架中的对象
2)过滤器实现Filter接口的对象, 拦截器是实现HandlerInterceptor
3)过滤器是用来设置request,response的参数,属性的,侧重对数据过滤的。
拦截器是用来验证请求的,能截断请求。
4)过滤器是在拦截器之前先执行的。
5)过滤器是tomcat服务器创建的对象
拦截器是springmvc容器中创建的对象
6)过滤器是一个执行时间点。
拦截器有三个执行时间点
7)过滤器可以处理jsp,js,html等等
拦截器是侧重拦截对Controller的对象。 如果你的请求不能被DispatcherServlet接收, 这个请求不会执行拦截器内容
8)拦截器拦截普通类方法执行,过滤器过滤servlet请求响应