Spring Boot参考教程(六)Spring Boot配置Servlet,Filter,Listener,Interceptor
来源:互联网 发布:超星尔雅网络课程 编辑:程序博客网 时间:2024/05/21 09:30
4.2.5. Spring Boot配置Servlet,Filter,Listener,Interceptor
1. 配置Servlet
在开发中一些场景常会使用到servlet,如用户登出等.下面演示如何进行配置.
配置使用Servlet有两种方式,一种时使用注解方式,一种使用配置类的方式。
配置Servlet有两种方式可选:
(1)注解配置Servlet
编写一个简单的servlet访问此Servlet时跳转至主页,在Servlet上加入注解@WebServlet(value = "/ConfigServlet"):
在Application类上加入注解@ServletComponentScan
启动后访问测试:
访问:
访问结果:
跳转成功!
(2)代码配置Servlet
使用上中servlet代码将注解注释后添加如下代码:
测试成功!
2. 配置Filter
配置Filter类似配置Servlet有两种配置方式可选:
(1)注解配置Filter
编写一个Filter加入注解@WebFilter("/*"):
在Application类上加注解@ServletComponentScan
启动访问:
访问结果:
注:对于Filter的更详细配置可查看注解@WebFilter("/*")中配置属性,例如配置Filter的过滤顺序等。
(2)代码配置Filter
注释上文中注解后编写配置代码:
注:FilterRegistrationBean.setOrder(0);方法为设置过滤器在过滤器链中顺序的方法。
不再测试!
3. 配置Listener
同Servlet,Fiter配置Listener也有两种方式:
(1)注解配置Listener
代码如下:
注:自定义配置Listener时可选的Listener有如下列表:
再Application类上加入注解:@ServletComponentScan
启动测试:
(2)代码配置Listener
注释上述注解,编写代码:
不再测试!
4. 配置Interceptor
编写拦截器:
编写配置类:
测试:
访问任何可用请求:
<实例代码>
Github:https://github.com/chunyuding/SpringBoot-Demo
https://github.com/chunyuding/SpringBoot-Demo.git
码云:https://gitee.com/dingchunyu/SpringBoot-Demo
https://gitee.com/dingchunyu/SpringBoot-Demo.git
<推荐书籍>
百度云:http://pan.baidu.com/s/1qYA0Nxi
- Spring Boot参考教程(六)Spring Boot配置Servlet,Filter,Listener,Interceptor
- spring boot (二) servlet listener filter interceptor
- spring boot-servlet、filter、listener
- Spring-Boot学习笔记(三) 编写Servlet、Filter、Listener、Interceptor
- spring boot学习8之Servlet/Filter/Listener配置
- spring boot(18)-servlet、filter、listener
- spring boot(18)-servlet、filter、listener
- Spring boot 配置Servelt、Filter、Listener
- 18.玩转Spring Boot 注册Servlet、Filter、Listener
- spring-boot如何定义一个servlet,filter,listener
- spring boot servlet filter linstener
- Spring Boot参考教程(五)Spring Boot配置使用之配置类用法
- Spring Boot Servlet配置
- Spring Boot参考教程(九)配置上传下载
- Spring Boot参考教程(四)Spring Boot配置使用之配置文件用法
- spring boot第三节 Filter和Listener
- Spring Boot构建interceptor简明教程
- spring boot 配置Filter过滤器
- 九度OJ题目1028:继续畅通工程
- 自定义ROM卡刷包教程
- Qt 使用QuaZip库生成MD5加密Zip格式压缩文件
- HDFS原理解析(总体架构,读写操作流程)
- HDFS体系结构
- Spring Boot参考教程(六)Spring Boot配置Servlet,Filter,Listener,Interceptor
- HDFS追本溯源:体系架构详解
- 通俗理解卷积
- 【实战】2-25 git安装配置
- java夯实基础-容器
- [LintCode]413.反转整数
- thinkcmf本地安装及线上安装要点
- Matlab程序入门GUI项目之二:简单科学计算器
- linux中与 信号量有关的函数