springboot-过滤器-拦截器-监听-servlet

引入servlet依赖

springboot-过滤器-拦截器-监听-servlet_第1张图片


   javax.servlet
    javax.servlet-api
    4.0.1
    provided

启动类

启动类添加扫描注解

@SpringBootApplication
@ServletComponentScan
public class TestinterceptorApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestinterceptorApplication.class, args);
    }

}

testfilter

package com.interceptor.testinterceptor.filter;

import org.springframework.web.filter.ServletContextRequestLoggingFilter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(urlPatterns = "/*")
public class testfilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("进入过滤器初始化");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入过滤器");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("进入过滤器销毁");
    }
}

TestInterceptor

package com.interceptor.testinterceptor.interceptor;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

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

public class TestInterceptor extends HandlerInterceptorAdapter {

    @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("=======postHandle========");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("=======afterCompletion========");
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("=======afterConcurrentHandlingStarted========");
    }
}

testlistener

package com.interceptor.testinterceptor.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class testlistener implements ServletContextListener {
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("销毁监听");
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("初始化监听");
    }
}

testservlet

package com.interceptor.testinterceptor.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(urlPatterns = “/myservlet”)
public class testservlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(“调用了get方法”);
this.doPost(req,resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("调用了dopost方法");
}

}

你可能感兴趣的:(springboot,servlet,过滤器,interceptor,filter,spring)