[web]Servlet中的Listener和Filter
建议先看看 ——> Servlet工作原理
一、Listener
在Tomcat服务中,Listener的设计是基于观察者模式的,目前在Servlet中提供6中两类事件的观察者接口,它们分别是:
实际上这六个Listener都继承了EvenListener,每个Listener由分别定义了自己需要实现的接口:
ServletContextAttributeListener:
public interface ServletContextAttributeListener extends EventListener {
// 设置属性时
public default void attributeAdded(ServletContextAttributeEvent scae) {
}
// 删除属性时
public default void attributeRemoved(ServletContextAttributeEvent scae) {
}
// 、更改属性时
public default void attributeReplaced(ServletContextAttributeEvent scae) {
}
}
ServletRequestAttributeListener:
public interface ServletRequestAttributeListener extends EventListener {
// 更改Request属性时
public default void attributeAdded(ServletRequestAttributeEvent srae) {
}
// 更改Request属性时
public default void attributeRemoved(ServletRequestAttributeEvent srae) {
}
// 更改Request属性时
public default void attributeReplaced(ServletRequestAttributeEvent srae) {
}
}
HttpSessionAttributeListener:
public interface HttpSessionAttributeListener extends EventListener {
// 添加 Session 属性时
public default void attributeAdded(HttpSessionBindingEvent se) {
}
// 删除 Session 属性时
public default void attributeRemoved(HttpSessionBindingEvent se) {
}
// 更改 Session 属性时
public default void attributeReplaced(HttpSessionBindingEvent se) {
}
}
ServletRequestAttributeListener:
public interface ServletRequestAttributeListener extends EventListener {
public default void attributeAdded(ServletRequestAttributeEvent srae) {
}
public default void attributeRemoved(ServletRequestAttributeEvent srae) {
}
public default void attributeReplaced(ServletRequestAttributeEvent srae) {
}
}
ServletContextListener:
public interface ServletContextListener extends EventListener {
// StandardConetxt 创建时
public default void contextInitialized(ServletContextEvent sce) {
}
// StandardConetxt 销毁时
public default void contextDestroyed(ServletContextEvent sce) {
}
}
HttpSessionListener:
public interface HttpSessionListener extends EventListener {
public default void sessionCreated(HttpSessionEvent se) {
}
public default void sessionDestroyed(HttpSessionEvent se) {
}
}
二、Filter
Filter可以完成和Servlet同样的工作,除了request和response两个对象外,它还提供了一个FilterChain对象,它可以帮助我们更加灵活地控制请求的流程,该接口定义如下:
Filter:
public interface Filter {
// 初始化接口,在用户定义的Filter初始化时被调用,它与Servlet的init方法是一样的,
// FilterConfig也和ServletConfig类似,除了可以获取ServletContext之外,还可以在配置文件中声明参数
public default void init(FilterConfig filterConfig) throws ServletException {}
// 每个用户的匹配请求进来时,这个方法都被调用,并且是在Servlet的service方法之前被调用,
// 而 FilterChain代表了当前的整个请求链,通过调用FilterChain.doFilter可以将请求继续传递下去,
// 如果需要拦截该请求,那么就不调用FilterChain,就可以直接返回了,所以它是一种责任链模式
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException;
// Filter被销毁时,会调用这个方法,web容器调用这个方法之后,容器会再调用一次doFilter方法
public default void destroy() {}
}

除此之外还有两个接口的定义,分别FilterConfig 和FilterChain
FilterConfig:
public interface FilterConfig {
/**
* Get the name of the filter.
*
*/
public String getFilterName();
/**
* Returns a reference to the {@link ServletContext} in which the caller is
* executing.
*
*/
public ServletContext getServletContext();
/**
* Returns a <code>String</code> containing the value of the named
* initialization parameter, or <code>null</code> if the parameter does not
* exist.
*
*/
public String getInitParameter(String name);
/**
* Returns the names of the filter's initialization parameters as an
*/
public Enumeration<String> getInitParameterNames();
}
FilterChain:
public interface FilterChain {
/**
* Causes the next filter in the chain to be invoked, or if the calling
* filter is the last filter in the chain, causes the resource at the end of
* the chain to be invoked.
*
*/
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException;
}
三、<url-pattern>匹配规则
- 精确匹配:如/foo/.html,只能匹配/foo.html
- 路径匹配:如/foo/*,将匹配/foo/路径下所有的URL
- 后缀匹配:如*.html,将匹配所有所有的后缀为.html的路径
匹配的顺序也是:精确匹配——> 最长路径匹配——>后缀匹配
[web]Servlet中的Listener和Filter的更多相关文章
- web.xml 中的listener、 filter、servlet 加载顺序及其详解
在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...
- web.xml 中的listener、 filter、servlet 加载顺序
在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...
- web.xml 中的listener、filter、servlet 加载顺序及其【配置详解】
在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...
- 转:web.xml 中的listener、 filter、servlet 加载顺序及其详解
在项目中总会遇到一些关于加载的优先级问题,刚刚就遇到了一个问题,由于项目中使用了quartz任务调度,quartz在web.xml中是使用listener进行监听的,使得在tomcat启动的时候能马上 ...
- web.xml 中的listener、 filter、servlet 加载顺序及其详解(转)
在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...
- [Java][Web] Servlet中转发和重定向比较
Servlet中页面跳转的两种方式 请求转发 使用requestDispatcher对象 request.getRequestDispatcher("path").forward( ...
- web.xml中自定义Listener
Listener可以监听容器中某一执行动作,并根据其要求做出相应的响应. 常用的Web事件的监听接口如下: ServletContextListener:用于监听Web的启动及关闭 ServletCo ...
- Java Web servlet中的cookie
点击submit后: 点击查看Cookies: 在C:\Documents and Settings\Administrator\Cookies目录下面会有一个 hongten@webproj ...
- 一个web项目中web.xml<context-param>的作用
转 <context-param>的作用:web.xml的配置中<context-param>配置作用1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置 ...
随机推荐
- 微信小程序文档里看不到的小Tips
前几天折腾了下.然后列出一些实验结果,供大家参考. 0. 使用开发工具模拟的和真机差异还是比较大的.也建议大家还是真机调试比较靠谱. 1. WXML(HTML) 1.1 小程序的WXML没有HTML的 ...
- SpringBoot28 RabbitMQ知识点、Docker下载RabbitMQ、SpringBoot整合RabbtiMQ
1 RabbitMQ知识点 1.1 整体架构图 消息生产者将消息投递到exchange中,exchange会以某种路由机制将生产者投递的消息路由到queue中,消息消费者再从queue中获取消息进行消 ...
- Linux静态库与动态库详解
引言 为了代码的复用性和模块化,我们常常使用一些库文件,在Windows操作系统下位.lib .dll作为静态库和动态库的后缀名. 在Linux下,静态链接库名字一般为libabcdef.a,其中ab ...
- Spring Boot☞ 使用freemarker模板引擎渲染web视图
效果图 代码 package com.wls.integrateplugs.hello.controller; /** * Created by wls on 2017/8/24. */ import ...
- ES5、6、7浅析
ECMA Script 它是一种由ECMA组织(前身为欧洲计算机制造商协会)制定和发布的脚本语言规范 而我们学的JavaScript是ECMA的实现, 但术语ECMAScript和JavaScript ...
- Yii2 中国省市区三级联动
1.获取源码:https://github.com/chenkby/yii2-region 2.安装 添加到你的composer.json文件 "chenkby/yii2-region&qu ...
- ubuntu虚拟机安装简单pxe服务器
安装环境: vmware2台虚拟机,一台用来做pxe服务器(安装tftp.dhcp.apache2等服务),另一台用来做安装测试. 虚拟机的网络配置:dhcp的虚拟网络为192.168.10.100/ ...
- Verilog MIPS32 CPU(三)-- ALU
Verilog MIPS32 CPU(一)-- PC寄存器 Verilog MIPS32 CPU(二)-- Regfiles Verilog MIPS32 CPU(三)-- ALU Verilog M ...
- Arduino I2C + 三轴加速度计ADXL345
ADXL345是ADI公司生产的三轴数字加速度计芯片,与ST的LIS3DH功能接近.主要特性有: 工作电压:2.0 ~ 3.6V 功耗:待机功耗0.1μA:工作时与数据输出频率(ODR)有关,如ODR ...
- Replication--复制笔记1
1.快照复制和事务复制使用分发代理传递文件,而合并复制使用合并代理来传递文件2.快照代理在分发服务器上运行3.在创建快照是,根据复制的类型对发布表的加锁方式而不同 a)对应合并发布,快照代理不适 ...