本文共 4816 字,大约阅读时间需要 16 分钟。
监听器:listener是servlet规范中定义的一种特殊类。用于监听servletContext、HttpSession和servletRequest等域对象的创建和销毁事件。监听域对象的属性发生修改的事件。用于在事件发生前、发生后做一些必要的处理。其主要可用于以下方面:1、统计在线人数和在线用户2、系统启动时加载初始化信息3、统计网站访问量4、记录用户访问路径。
过滤器:Filter是Servlet技术中最实用的技术,Web开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。它主要用于对用户请求进行预处理,也可以对HttpServletResponse进行后处理。使用Filter的完整流程:Filter对用户请求进行预处理,接着将请求交给Servlet进行处理并生成响应,最后Filter再对服务器响应进行后处理。
拦截器:Interceptor 在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。比如日志,安全等。一般拦截器方法都是通过动态代理的方式实现。可以通过它来进行权限验证,或者判断用户是否登陆,或者是像12306 判断当前时间是否是购票时间。
代码示例:
public class DemoFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println(servletRequest.getParameter("name")); HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest; HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) servletResponse); if(httpServletRequest.getRequestURI().indexOf("/index") != -1 || httpServletRequest.getRequestURI().indexOf("/asd") != -1 || httpServletRequest.getRequestURI().indexOf("/online") != -1 || httpServletRequest.getRequestURI().indexOf("/login") != -1 ) { filterChain.doFilter(servletRequest, servletResponse); }else { wrapper.sendRedirect("/login"); } } @Override public void destroy() { } @Override public void init(FilterConfig filterConfig) throws ServletException { }}
public class DemoHttpSessionListener implements HttpSessionListener { public static int online = 0; @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("listener------------------创建session"); online ++; } @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("listener------------------销毁session"); }}
public class DemoInterceptor implements HandlerInterceptor { //在请求处理之前进行调用 @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("preHandle被调用"); Map map =(Map)httpServletRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); System.out.println(map.get("name")); System.out.println(httpServletRequest.getParameter("username")); if(map.get("name").equals("zhangsan")) { return true; //如果false,停止流程,api被拦截 }else { PrintWriter printWriter = httpServletResponse.getWriter(); printWriter.write("please login again!"); return false; } } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle被调用"); } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion被调用"); }}
@Configurationpublic class WebMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/zxc/foo").setViewName("foo"); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new DemoInterceptor()) .addPathPatterns("/asd/**"); System.out.println("添加----------------------------interceptor"); } @Bean public FilterRegistrationBean filterRegist() { FilterRegistrationBean frBean = new FilterRegistrationBean(); frBean.setFilter(new DemoFilter()); frBean.addUrlPatterns("/*"); System.out.println("添加----------------------------filter"); return frBean; } @Bean public ServletListenerRegistrationBean listenerRegist() { ServletListenerRegistrationBean srb = new ServletListenerRegistrationBean(); srb.setListener(new DemoHttpSessionListener()); System.out.println("添加----------------------------listener"); return srb; }}
或通过注解的方式添加,如:
过滤器所在类添加@WebFilter(urlPatterns = "/*", filterName = "reqResFilter")启动类添加@ServletComponentScan
如:
@WebListener//标识这是servelt的监听器启动类添加@ServletComponentScan("com.hzt")//servlet监听器的使用
在springboot 应用中,使用监听器有servlet容器的,有spring的。servlet是:public class DemoServletListener implements ServletContextListener
spring是:public class DemoSpringListener implements ApplicationListener<ApplicationEvent>
看springboot启动时,监听自己的事件,servlet监听事件早于spring的.
转载地址:https://blog.csdn.net/leijie0322/article/details/109899022 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!