GitHub 地址:
https://github.com/asd821300801/Spring-Boot.git
在src下创建com.example.filter包
package com.example.filter;
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;
import javax.servlet.annotation.WebFilter;
/**
* Servlet Filter implementation class ServletFilter
* 注解注册过滤器:实现 javax.servlet.Filter接口
* filterName 是过滤器的名字
* urlPatterns 是需要过滤的请求 ,这里只过滤servlet/* 下面的所有请求
*/
@WebFilter(filterName="ServletFilter",urlPatterns="/servlet/*")
public class ServletFilter implements Filter {
/**
* Default constructor.
*/
public ServletFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
System.out.println("过滤器被销毁。");
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
System.out.println("过滤器正在执行...");
// pass the request along the filter chain
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
System.out.println("初始化过滤器。");
}
}
测试
http://localhost:8080/servlet/servlet2.action
控制台输出:
在src下创建com.example.listener包
package com.example.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* Application Lifecycle Listener implementation class ContextListener
* @author LingDu
*/
@WebListener
public class ContextListener implements ServletContextListener {
/**
* Default constructor.
*/
public ContextListener() {
// TODO Auto-generated constructor stub
}
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("监听器被销毁");
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("监听器初始化。");
}
}
项目启动的时候 监听器是先被初始化的
在src下创建com.example.interceptor包,
创建MyInterceptor.java类,实现HandlerInterceptor接口,重写里面3个主要的方法。
package com.example.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
* 1、创建拦截器类并实现 HandlerInterceptor 接口
* @author LingDu
*/
public class MyInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("拦截器MyInterceptor------->3、请求结束之后被调用,主要用于清理工作。");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("拦截器MyInterceptor------->2、请求之后调用,在视图渲染之前,也就是Controller方法调用之后");
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("拦截器MyInterceptor------->1、请求之前调用,也就是Controller方法调用之前。");
return true;//返回true则继续向下执行,返回false则取消当前请求
}
}
在src下创建com.example.interceptor包
创建InterceptorConfigurerAdapter类,继承WebMvcConfigurerAdapter类,重写addInterceptors方法
注意:需要加上@Configuration注解
package com.example.interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 创建java类继承WebMvcConfigurerAdapter类,重写addInterceptors方法
* @author LingDu
*/
@Configuration
public class InterceptorConfigurerAdapter extends WebMvcConfigurerAdapter {
/**
* 该方法用于注册拦截器
* 可注册多个拦截器,多个拦截器组成一个拦截器链
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns 添加路径
// excludePathPatterns 排除路径
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/*.*");
super.addInterceptors(registry);
}
}
页面访问:
http://localhost:8080/page1.action
成功拦截: