• springboot整合intercept、filter、servlet


    整合intercept

    @Component
    public class MyInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            System.out.println("preHandle");
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            System.out.println("postHandler");
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            System.out.println("afterCompletion");
        }
    }
    

    整合Filter

    public class MyFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            System.out.println("doFilter");
            filterChain.doFilter(servletRequest, servletResponse);
        }
    
        @Override
        public void destroy() {
        }
    }
    

    整合Servlet

    public class MyServlet extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("hello......");
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req, resp);
        }
    }
    
    

    注册类

    @Configuration
    public class AppConfig extends WebMvcConfigurerAdapter {
        @Autowired
        MyInterceptor myInterceptor;
    
        /**
         * 注册拦截器
         *
         * @param registry
         */
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/index.html", "/");
        }
    
        /**
         * 注册过滤器
         *
         * @return
         */
        @Bean
        public FilterRegistrationBean myFilter() {
            FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
            filterRegistrationBean.setFilter(new MyFilter());
            filterRegistrationBean.addUrlPatterns("/*");
            return filterRegistrationBean;
        }
    
        /**
         * 注册servlet
         *
         * @return
         */
        @Bean
        public ServletRegistrationBean myServlet() {
            ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
            servletRegistrationBean.setServlet(new MyServlet());
            servletRegistrationBean.addUrlMappings("/myServlet");
            return servletRegistrationBean;
        }
    }
    
    
  • 相关阅读:
    编译原理 十
    使用HighCharts实现实时数据展示
    img图片元素下多余空白解决方案
    jquery 滚动加载
    SVN服务器配置
    匿名对象和object的转换
    新浪微博开放平台OAuth授权解决方案(含代码)
    QQ互联OAuth2.0 .NET SDK 发布以及网站QQ登陆示例代码
    Senparc.Weixin.MP SDK 微信公众平台开发教程(七):解决用户上下文(Session)问题
    Senparc.Weixin.MP SDK 微信公众平台开发教程(六):了解MessageHandler
  • 原文地址:https://www.cnblogs.com/yinchh/p/12547864.html
Copyright © 2020-2023  润新知