(21)Spring Boot过滤器、监听器【从零开始学Spring Boot】
Spring Boot 系列博客】
(0)前言【从零开始学Spring Boot】 :
http://412887952-qq-com.iteye.com/blog/2291496
(1)spring boot起步之Hello World【从零开始学Spring Boot】:
http://412887952-qq-com.iteye.com/blog/2291500
(2)Spring Boot返回json数据【从零开始学Spring Boot】
http://412887952-qq-com.iteye.com/blog/2291508
…
(16)Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】
http://412887952-qq-com.iteye.com/blogs/2292376
(17)Spring Boot普通类调用bean【从零开始学Spring Boot】:
http://412887952-qq-com.iteye.com/blog/2292388
更多查看博客:http://412887952-qq-com.iteye.com/blog
上一篇文章已经对定义Servlet 的方法进行了说明,过滤器(Filter)和监听器(Listener)的注册方法和 Servlet 一样,不清楚的可以查看下上一篇文章(20): 本文将直接使用@WebFilter和@WebListener的方式,完成一个Filter 和一个 Listener;使用注解
@ServletComponentScan//这个就是扫描相应的Servlet包;
过滤器(Filter)文件
com.kfit.filter.MyFilter.java
package com.kfit.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;
/**
*
* 使用注解标注过滤器
* @WebFilter将一个实现了javax.servlet.Filter接口的类定义为过滤器
* 属性filterName声明过滤器的名称,可选
* 属性urlPatterns指定要过滤的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性)
* @author Angel(QQ:412887952)
* @version v.0.1
*/
@WebFilter(filterName="myFilter",urlPatterns="/*")
publicclass MyFilter implements Filter{
@Override
publicvoid init(FilterConfig config) throws ServletException {
System.out.println("过滤器初始化");
}
@Override
publicvoid doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("执行过滤操作");
chain.doFilter(request, response);
}
@Override
publicvoid destroy() {
System.out.println("过滤器销毁");
}
}
ServletContext监听器(Listener)文件
com.kfit.listener.MyServletContextListener:
package com.kfit.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* 使用@WebListener注解,实现ServletContextListener接口
*
* @author Angel(QQ:412887952)
* @version v.0.1
*/
@WebListener
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContex销毁");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("ServletContex初始化");
}
}
ServletContext监听器(Listener)文件(HttpSessionListener)
MyHttpSessionListener.java
package com.kfit.listener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* 监听Session的创建与销毁
*
*/
@WebListener
publicclassMyHttpSessionListenerimplementsHttpSessionListener {
@Override
publicvoid sessionCreated(HttpSessionEvent se) {
System.out.println("Session 被创建");
}
@Override
publicvoid sessionDestroyed(HttpSessionEvent se) {
System.out.println("ServletContex初始化");
}
}
注意不要忘记在 SpringBootSampleApplication.java 上添加 @ServletComponentScan 注解。
启动的过程中我们会看到输出:
ServletContex初始化
过滤器初始化
服务启动后,随便访问一个页面,会看到输出:
执行过滤操作
Session
被创建
为什么无法看到session的过程:http://zhidao.baidu.com/link?url=EP-wlLvKpo8zI5NaIZrESzCdivq3Xg8VgOWQOvfpSLl3opTgvESerpo4wsG6tOs_dm6cQQMF_kQ6THNjNzr2Nq
至于如何使用代码的方式注册Filter和Listener,请参考上一篇文章关键Servlet的介绍。不同的是需要使用 FilterRegistrationBean 和 ServletListenerRegistrationBean 这两个类。
(21)Spring Boot过滤器、监听器【从零开始学Spring Boot】的更多相关文章
- 63.JPA/Hibernate/Spring Data概念【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 事情的起源,无意当中在一个群里看到这么一句描述:"有人么?默默的问一句,现在开发用mybatis还是hibernate还是jpa&quo ...
- 47. Spring Boot发送邮件【从零开始学Spring Boot】
(提供源代码) Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Bo ...
- 20. Spring Boot Servlet【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52069482 Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可 ...
- (20)Spring Boot Servlet【从零开始学Spring Boot】
Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet.Filter.Listener.Interceptor 等等. 当使用Spring-Boot时,嵌 ...
- 21. Spring Boot过滤器、监听器【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52069490 上一篇文章已经对定义Servlet 的方法进行了说明,过滤器(Filter) ...
- (39.2). Spring Boot Shiro权限管理【从零开始学Spring Boot】
(本节提供源代码,在最下面可以下载) (4). 集成Shiro 进行用户授权 在看此小节前,您可能需要先看: http://412887952-qq-com.iteye.com/blog/229973 ...
- 68. 使用thymeleaf报异常:Not Found, status=404【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 我们按照正常的流程编码好了 controller访问访问方法/hello,对应的是/templates/hello.html文件,但是在页面中还是 ...
- 67. @Transactional的类注入失败【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] Spring的代理模式有两种:java自带的动态代理模式和cglib代理模式,cglib代码模式适用于没有接口的类,而java自带适用于接口类,默 ...
- (39.1) Spring Boot Shiro权限管理【从零开始学Spring Boot】
(本节提供源代码,在最下面可以下载)距上一个章节过了二个星期了,最近时间也是比较紧,一直没有时间可以写博客,今天难得有点时间,就说说Spring Boot如何集成Shiro吧.这个章节会比较复杂,牵涉 ...
- (22)Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】
上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API,与Spring 没什么关系. Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Sprin ...
随机推荐
- oc26--Property,省略setget的声明
// // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { int _age; } /* ...
- C# 读取ini文件 百度问问学习文档
C# 读取ini文件 10 有多个section,现想读取整个ini文件和指定section下所有内容 补充: 发布答案可以,请对准题目啊,我不要指定节点的内容,我知道!我要的是读取指定区域的内容,假 ...
- [Apple开发者帐户帮助]六、配置应用服务(1.2)Apple Pay:在网络上配置Apple Pay
网上Apple Pay允许用户在您的网络应用中购买商品和服务. 首先在您的开发者帐户中创建一个商家标识符,该标识符可以将Apple Pay唯一标识为能够接受付款的商家.您可以为多个本机和Web应用程序 ...
- MSSQL:账号无法删除方案
1.查询 EXEC sp_who 'WIN-GBKBCVTG4CN\Administrator' 返回一个表格,其中有列[spid] 2.删除 kill spid
- python 11:range(起始索引,终止索引,步数)(默认情况下步数为1,生成从起始索引,每次增加(终止索引-起始索引)/步数,到终止索引前的数字串)
squares = [] for value in range(1,11): #第三参数默认为1,生成从1开始,每次增加1步数,到11前的10为止的数字串 square = value ** 2 sq ...
- SQLServer2008 使用sql语句访问excel数据
exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc Distributed Quer ...
- Vue初级
来学习vue.js的宝宝们应该都了解了什么是vue并且是有兴趣去学的,我也是自己学习vue,欢迎大家一起讨论. 现在,我么先自己尝试用vue来写个hello world吧. 一.创建一个 .html ...
- 用JSP实现动态交互
一.什么是JSP? 1.在HTML中嵌入Java脚本代码 2.由应用服务器中的JSP引擎来编译和执行嵌入的Java脚本代码 3.然后将生成的整个页面信息返回给客户端 二.为什么需要基于B/S技术的 ...
- 【转】linux命令
shell实例手册 0 说明{ 手册制作: 雪松 更新日期: 2015-11-02 欢迎系统运维加入Q群: 198173206 # 加群请回答问题 欢迎运维开发加入Q群: 3655344 ...
- iproute2常用命令
#常用命令 ip link show #显示链路 ip addr show #显示地址(或ifconfig) ip route show #显示路由(route -n) ip neigh show # ...