zoukankan      html  css  js  c++  java
  • java 过滤器(Filter)与springMVC 拦截器(interceptor)的实现案例

    java 过滤器Filter:

    package com.sun.test.aircraft.filter;
    import javax.servlet.*;
    import java.io.IOException;
    public class SunnyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    System.out.println("init success");
    System.out.println("FilterName:" + filterConfig.getFilterName() + " " + "InitParameterNames:" + filterConfig.getInitParameterNames() + " " + "ServletContext:" + filterConfig.getServletContext());
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    System.out.println("start filter");
    chain.doFilter(request, response);
    }
    @Override
    public void destroy() {
    System.out.println("destroy enheng!");
    }
    }

    web.xml配置:
    <filter>
    <filter-name>sunnyFilter</filter-name>
    <filter-class>com.sun.test.aircraft.filter.SunnyFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>sunnyFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    结果:

    =====================================================================================================================

    springMVC 拦截器interceptor:
    package com.sun.test.aircraft.intercept;

    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class SunnyIntercept implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
    System.out.println("interceptor before working");
    return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    System.out.println("interceptor working");
    String str="interceptor working";
    modelAndView.addObject("incept",str);
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    System.out.println("interceptor worked done");
    }
    }
    spring-mvc.xml配置
    <mvc:interceptors>
    <mvc:interceptor>
    <mvc:mapping path="/hello/**"/>
    <bean class="com.sun.test.aircraft.intercept.SunnyIntercept"/>
    </mvc:interceptor>
    </mvc:interceptors>
    package com.sun.test.aircraft.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;

    controller实现(可以理解为实现的do)
    @Controller
    @RequestMapping("/hello")
    public class HelloController {

    @RequestMapping("/u")
    public void hello() {
    System.out.println("hello you");
    }
    }
    结果:

     一句话:过滤器:“取你所想取,过滤的是链接,关注的是web请求;拦截器:“拒你所想拒”关注的是业务逻辑{业务逻辑之前,业务逻辑正在,业务逻辑之后}所处理的事情

    
    
  • 相关阅读:
    struct2 学习总结
    c++ 容器(list学习总结)
    java 网络编程(五)----TCP进阶篇上传文本文件
    java 网络编程(四)----UDP进阶篇聊天小程序
    java 网络编程(三)---TCP的基础级示例
    java 网络编程(二)----UDP基础级的示例
    java 网络编程(一)---基础知识和概念了解
    GitHub和git和repo的使用
    android studio不能预览
    关于android studio2.3和android studio3.0
  • 原文地址:https://www.cnblogs.com/austinspark-jessylu/p/7699151.html
Copyright © 2011-2022 走看看