springboot添加对listener,servlet,filter的支持
比较常用的方式就是使用注解来添加对 监听器,过滤器,servlet的支持。
1.首先在启动类上添加 @ServletComponentScan 开启 对监听器,过滤器,servlet的注解扫描。
分别创建过滤器,拦截器,servlet
- package com.example.demo.filter;
- import java.io.IOException;
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.annotation.WebFilter;
- @WebFilter(urlPatterns= {"/*"})
- public class MyFilter implements Filter {
- @Override
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
- throws IOException, ServletException {
- System.out.println("my filter....");
- chain.doFilter(request, response);
- }
- }
- package com.example.demo.listener;
- import javax.servlet.annotation.WebListener;
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
- @WebListener(value="MySessionListener")
- public class MySessionListener implements HttpSessionListener {
- @Override
- public void sessionCreated(HttpSessionEvent se) {
- HttpSessionListener.super.sessionCreated(se);
- System.out.println("session create...");
- }
- @Override
- public void sessionDestroyed(HttpSessionEvent se) {
- HttpSessionListener.super.sessionDestroyed(se);
- }
- }
- package com.example.demo.servlet;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- @WebServlet(urlPatterns="/my1",name="my1")
- public class MyServlet extends HttpServlet {
- private static final long serialVersionUID = 4322324790077226450L;
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- System.out.println("MyServlet.....");
- req.getSession().setAttribute("aa", "bb");
- }
- @Override
- protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- super.doPut(req, resp);
- }
- }
访问servlet结果如下:
my filter....
MyServlet.....
session create...
springboot添加对listener,servlet,filter的支持的更多相关文章
- SpringBoot使用拦截器/ Servlet/ Filter
一.SpringBoot中使用拦截器 使用SpringMVC的拦截器,需要定义好拦截器,然后通过配置文件文件,对其进行注册 而在SpringBoot项目中,之前在配置文件中配置的内容,现在体现在一个类 ...
- SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式
在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式. 1. 使用Bean ...
- ServletContextInitializer添加 servlet filter listener
ServletContextInitializer添加 servlet filter listener https://www.cnblogs.com/pomer-huang/p/9639322.ht ...
- SpringBoot整合WEB开发--(九)整合Servlet,Filter,Listener
简介: 如果需要整合第三方框架时,可能还是不得不使用Servlet,Filter,Listener,Springboot中也有提供支持. @WebServlet("/my") pu ...
- 基于注解的SpringMVC添加其他的Servlet、Filter以及Listener
我们可以在AbstractAnnotationConfigDispatcherServletInitializer的实现类中重写onStartup(ServletContext servletCont ...
- springboot扫描自定义的servlet和filter代码详解_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 这几天使用spring boot编写公司一个应用,在编写了一个filter,用于指定编码的filter,如下: /** ...
- servlet/filter/listener/interceptor区别与联系
转自:http://www.cnblogs.com/doit8791/p/4209442.html servlet.filter.listener是配置到web.xml中(web.xml 的加载顺序是 ...
- java web.xml listener servlet 和filter加载顺序
在该项目中总会遇到一些关于加载的优先问题.最近遇到了同样的类别似的,所以,如果你发现信息汇总下,以下是转载其他一些人,毕竟,人们写的不错.它不重复创建的轮.只是略作修改自己的观点. 首先能够肯定的是, ...
- 【转】servlet/filter/listener/interceptor区别与联系
原文:https://www.cnblogs.com/doit8791/p/4209442.html 一.概念: 1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台 ...
随机推荐
- [容器]docker-ce安装最新版-docker常用操作
社区: http://www.dockerinfo.net/rancher http://dockone.io/ https://www.kubernetes.org.cn/ 1,docker安装配置 ...
- wxPython 4.0.0b2安装
https://www.cnblogs.com/NanShan2016/p/5518235.html 亮的界面是一个GUI程序必不可少的一部分,wxPython可以做到这一点,加之Python强大的功 ...
- 680. Valid Palindrome II【easy】
680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...
- SpringBoot+MybatisPlus(多数据源和主从分离)
简介 dynamic-datasource-spring-boot-starter 基于 springBoot2.0. 它适用于读写分离,一主多从的环境. 主数据库使用 INSERT UPDATE D ...
- 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 ...
- php 防止刷新重复下载文件
在超链接中增加随机数. <a href="./index.php?module=operation&action=download&url=D:\WW\WlequPho ...
- java中.currentTimeMillis的用法和含义
用法:可以用法获取当前时间的毫秒数,可以通过毫秒数进行时间比较,时间转化以及时间格式化等.public class SystemTime {public static void main(String ...
- JavaScript 代码块
JavaScript 语句通过代码块的形式进行组合. 块由左花括号开始,由右花括号结束. 块的作用是使语句序列一起执行. JavaScript 函数是将语句组合在块中的典型例子. 下面的例子将运行可操 ...
- SlidingMenu官方实例分析1——ExampleListActivity
1.SlidingMenuDemo下载: 由AndroidManifest.xml能看出项目是从ExampleListActivity启动的: ExampleListActivity继承了Sherlo ...
- FIR滤波器与IIR滤波器
FIR(Finite Impulse Response)滤波器 有限长单位冲激响应滤波器,又称为非递归型滤波器 特点: FIR滤波器的最主要的特点是没有反馈回路,稳定性强,故不存在不稳定的问题: FI ...