比较常用的方式就是使用注解来添加对 监听器,过滤器,servlet的支持。

1.首先在启动类上添加  @ServletComponentScan  开启 对监听器,过滤器,servlet的注解扫描。

分别创建过滤器,拦截器,servlet

  1. package com.example.demo.filter;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.Filter;
  6. import javax.servlet.FilterChain;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.ServletRequest;
  9. import javax.servlet.ServletResponse;
  10. import javax.servlet.annotation.WebFilter;
  11.  
  12. @WebFilter(urlPatterns= {"/*"})
  13. public class MyFilter implements Filter {
  14.  
  15. @Override
  16. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  17. throws IOException, ServletException {
  18. System.out.println("my filter....");
  19. chain.doFilter(request, response);
  20. }
  21.  
  22. }

  

  1. package com.example.demo.listener;
  2.  
  3. import javax.servlet.annotation.WebListener;
  4. import javax.servlet.http.HttpSessionEvent;
  5. import javax.servlet.http.HttpSessionListener;
  6. @WebListener(value="MySessionListener")
  7. public class MySessionListener implements HttpSessionListener {
  8.  
  9. @Override
  10. public void sessionCreated(HttpSessionEvent se) {
  11. HttpSessionListener.super.sessionCreated(se);
  12. System.out.println("session create...");
  13. }
  14.  
  15. @Override
  16. public void sessionDestroyed(HttpSessionEvent se) {
  17. HttpSessionListener.super.sessionDestroyed(se);
  18. }
  19.  
  20. }

  

  1. package com.example.demo.servlet;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.ServletException;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. @WebServlet(urlPatterns="/my1",name="my1")
  12. public class MyServlet extends HttpServlet {
  13.  
  14. private static final long serialVersionUID = 4322324790077226450L;
  15.  
  16. @Override
  17. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. System.out.println("MyServlet.....");
  19. req.getSession().setAttribute("aa", "bb");
  20. }
  21.  
  22. @Override
  23. protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  24. super.doPut(req, resp);
  25. }
  26.  
  27. }

  访问servlet结果如下:

my filter....
MyServlet.....
session create...

springboot添加对listener,servlet,filter的支持的更多相关文章

  1. SpringBoot使用拦截器/ Servlet/ Filter

    一.SpringBoot中使用拦截器 使用SpringMVC的拦截器,需要定义好拦截器,然后通过配置文件文件,对其进行注册 而在SpringBoot项目中,之前在配置文件中配置的内容,现在体现在一个类 ...

  2. SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式

    在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式. 1. 使用Bean ...

  3. ServletContextInitializer添加 servlet filter listener

    ServletContextInitializer添加 servlet filter listener https://www.cnblogs.com/pomer-huang/p/9639322.ht ...

  4. SpringBoot整合WEB开发--(九)整合Servlet,Filter,Listener

    简介: 如果需要整合第三方框架时,可能还是不得不使用Servlet,Filter,Listener,Springboot中也有提供支持. @WebServlet("/my") pu ...

  5. 基于注解的SpringMVC添加其他的Servlet、Filter以及Listener

    我们可以在AbstractAnnotationConfigDispatcherServletInitializer的实现类中重写onStartup(ServletContext servletCont ...

  6. springboot扫描自定义的servlet和filter代码详解_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 这几天使用spring boot编写公司一个应用,在编写了一个filter,用于指定编码的filter,如下: /** ...

  7. servlet/filter/listener/interceptor区别与联系

    转自:http://www.cnblogs.com/doit8791/p/4209442.html servlet.filter.listener是配置到web.xml中(web.xml 的加载顺序是 ...

  8. java web.xml listener servlet 和filter加载顺序

    在该项目中总会遇到一些关于加载的优先问题.最近遇到了同样的类别似的,所以,如果你发现信息汇总下,以下是转载其他一些人,毕竟,人们写的不错.它不重复创建的轮.只是略作修改自己的观点. 首先能够肯定的是, ...

  9. 【转】servlet/filter/listener/interceptor区别与联系

    原文:https://www.cnblogs.com/doit8791/p/4209442.html 一.概念: 1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台 ...

随机推荐

  1. [容器]docker-ce安装最新版-docker常用操作

    社区: http://www.dockerinfo.net/rancher http://dockone.io/ https://www.kubernetes.org.cn/ 1,docker安装配置 ...

  2. wxPython 4.0.0b2安装

    https://www.cnblogs.com/NanShan2016/p/5518235.html 亮的界面是一个GUI程序必不可少的一部分,wxPython可以做到这一点,加之Python强大的功 ...

  3. 680. Valid Palindrome II【easy】

    680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...

  4. SpringBoot+MybatisPlus(多数据源和主从分离)

    简介 dynamic-datasource-spring-boot-starter 基于 springBoot2.0. 它适用于读写分离,一主多从的环境. 主数据库使用 INSERT UPDATE D ...

  5. c++ about SLL(Static-Link Library) and DLL(Dynamic-Link Library)

    First thing first, Wiki: http://en.wikipedia.org/wiki/Dynamic-link_library http://en.wikipedia.org/w ...

  6. php 防止刷新重复下载文件

    在超链接中增加随机数. <a href="./index.php?module=operation&action=download&url=D:\WW\WlequPho ...

  7. java中.currentTimeMillis的用法和含义

    用法:可以用法获取当前时间的毫秒数,可以通过毫秒数进行时间比较,时间转化以及时间格式化等.public class SystemTime {public static void main(String ...

  8. JavaScript 代码块

    JavaScript 语句通过代码块的形式进行组合. 块由左花括号开始,由右花括号结束. 块的作用是使语句序列一起执行. JavaScript 函数是将语句组合在块中的典型例子. 下面的例子将运行可操 ...

  9. SlidingMenu官方实例分析1——ExampleListActivity

    1.SlidingMenuDemo下载: 由AndroidManifest.xml能看出项目是从ExampleListActivity启动的: ExampleListActivity继承了Sherlo ...

  10. FIR滤波器与IIR滤波器

    FIR(Finite Impulse Response)滤波器 有限长单位冲激响应滤波器,又称为非递归型滤波器 特点: FIR滤波器的最主要的特点是没有反馈回路,稳定性强,故不存在不稳定的问题: FI ...