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】的更多相关文章

  1. 63.JPA/Hibernate/Spring Data概念【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 事情的起源,无意当中在一个群里看到这么一句描述:"有人么?默默的问一句,现在开发用mybatis还是hibernate还是jpa&quo ...

  2. 47. Spring Boot发送邮件【从零开始学Spring Boot】

    (提供源代码) Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Bo ...

  3. 20. Spring Boot Servlet【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52069482 Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可 ...

  4. (20)Spring Boot Servlet【从零开始学Spring Boot】

    Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet.Filter.Listener.Interceptor 等等. 当使用Spring-Boot时,嵌 ...

  5. 21. Spring Boot过滤器、监听器【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52069490 上一篇文章已经对定义Servlet 的方法进行了说明,过滤器(Filter) ...

  6. (39.2). Spring Boot Shiro权限管理【从零开始学Spring Boot】

    (本节提供源代码,在最下面可以下载) (4). 集成Shiro 进行用户授权 在看此小节前,您可能需要先看: http://412887952-qq-com.iteye.com/blog/229973 ...

  7. 68. 使用thymeleaf报异常:Not Found, status=404【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 我们按照正常的流程编码好了 controller访问访问方法/hello,对应的是/templates/hello.html文件,但是在页面中还是 ...

  8. 67. @Transactional的类注入失败【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] Spring的代理模式有两种:java自带的动态代理模式和cglib代理模式,cglib代码模式适用于没有接口的类,而java自带适用于接口类,默 ...

  9. (39.1) Spring Boot Shiro权限管理【从零开始学Spring Boot】

    (本节提供源代码,在最下面可以下载)距上一个章节过了二个星期了,最近时间也是比较紧,一直没有时间可以写博客,今天难得有点时间,就说说Spring Boot如何集成Shiro吧.这个章节会比较复杂,牵涉 ...

  10. (22)Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】

    上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API,与Spring 没什么关系.     Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Sprin ...

随机推荐

  1. Wikioi 1081 线段树成段更新单点查询

    线段树练习飘逸的写法,自从自己改成这样的写法之后,线段树就没再练过,如今最终练得上了. 由于这里查询仅仅是查询了叶子结点,所以pushUp函数就用不上了,只是我没去掉之前是3ms.去掉之后反而变成4m ...

  2. Fisher 线性判别

    Multiplying both sides of this result by wT and adding w0, and making use of y(x)=wTx+w0 and  y(xΓ)= ...

  3. 76.培训记录信息 Extjs 页面

    1.培训记录信息页面jsp <%@ page language="java" import="java.util.*" pageEncoding=&quo ...

  4. django的admin后台管理如何更改为中文

    新建Django的admin后端控制为英文显示,为了可以使其显示中文,可以将 setting.py配置文件修改 # LANGUAGE_CODE = 'en-us' # # # # # TIME_ZON ...

  5. vue移动端Ui组件 mint-ui 使用指南

    1.上啦加载下拉刷新的使用 this.$refs.loadmore.onTopLoaded(); this.$refs.loadmore.onBottomLoaded(); 上啦刷新下拉加载的 动画显 ...

  6. dpkg:处理软件包 mysql-server-5.5 (--configure)时出错

        卸载MySQL重新安装会出现如下问题:出现该问题主要是安装MySQL前需要删除 /var/lib/mysql文件夹以及/etc/mysql文件夹执行命令:    sudo rm /var/li ...

  7. hadoop 安装问题总结

    安装启动步骤  [英语好的,直接手把手跟着来] http://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/Sing ...

  8. 利用Xpath和jQuery进行元素定位示例

    利用Selenium在做前端UI自动化的时候,在元素定位方面主要使用了XPATH和jQuery两种方法.XPATH作为主要定位手段,jQuery作为补充定位手段.因为在通过XPATH进行定位的时候,S ...

  9. 微信小程序 PDF下载打印

    在开发微信小程序时,需要打印生成的PDF,实现思路是:后端生成相应的PDF,微信小程序下载并打开. 但是微信小程序并不可以打印,所以需要借助其他APP比如:WPS,但是发现微信小程序down的PDF在 ...

  10. BZOJ4756 [USACO17JAN]Promotion Counting晋升者计数

    Description The cows have once again tried to form a startup company, failing to remember from past ...