当前位置:网站首页>web三大组件之Filter、Listener
web三大组件之Filter、Listener
2022-04-23 13:07:00 【ArchieSean】
博主名取自《小羊肖恩》中的小羊肖恩,名字为:肖恩,音译为Sean,自己取的姓:阿奇,为符合我们的阅读习惯,连起来组成为ArchieSean。博主目前大三在读,志在将博客打造成为个人线上笔记的技术栈,方便自己也方便他人。如博客中有任何错误,请各位指出,谢谢大家。
Filter、Listener、Servlet统称为web三大组件。本篇只讲述前二,对于servlet将另开一篇进行详细介绍。对于Listener只做了解。
Filter
概念
web过滤器:在浏览器进行资源访问时,过滤器器将浏览器的请求拦截下来,用于完成一些特殊的功能。
作用:一般用于一些通用的操作。例如:登录验证、统一编码处理等。
基本使用
Filter的使用三步:
- 创建一个类实现Filter接口
- 复写方法
- 配置拦截路径(两种配置方式xml和注解配置)
相关配置
配置拦截路径:
方式1:xml配置
之前写servlet的时候,用xml配置有过这样一段xml配置:
<servlet>
<servlet-name>名称</servlet-name>
<servlet-class>全类名</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>名称</servlet-name>
<url-pattern>访问目录</url-pattern>
</servlet-mapping>
而filter的xml配置,和这个完全一致:
<filter>
<filter-name>名称</filter-name>
<filter-class>全类名</filter-class>
</filter>
<filter-mapping>
<filter-name>名称</filter-name>
<url-pattern>/*</url-pattern> <!-- 这里的路径并不是访问路径,而是对于拦截的路径的设置,/* 表示拦截所有--> </filter-mapping>
方式2:注解配置
@webFilter("拦截路径")
拦截流程
filter在拦截的时候,顺序如下:
- 执行拦截器
- 执行放行后的资源
- 执行放行语句下边的代码
生命周期
- init:在服务器启动后,会创建filter对象,调用init方法。只执行一次,用于加载资源。
- doFilter:每一次请求被拦截的资源时,该方法都会执行。
- destory:服务器关闭后,filter对象被销毁。服务器正常关闭,该方法会执行一次。用于释放资源。
过滤器链
如果有多个过滤器,执行顺序为:
例如:有三个过滤器,执行顺序如下:
- 过滤器1
- 过滤器2
- 过滤器3
- 资源执行
- 过滤器3
- 过滤器2
- 过滤器1
对于过滤器先后问题:
- 注解配置:
按照类名的字符串比较,小的先执行。 - xml配置:
<filter-mapping></fliter-mapping>顺序执行(由上到下)
Listener
概念
事件监听机制三要素:事件,事件源,监听器
简单来说就是,就是将三要素绑定在一起,当事件触发的时候,执行监听器的内容。
基本使用
- 定义一个类,实现ServletContextListener接口
- 复写方法
- 配置
3.1 web.xml:
<listener>
<listener-class>全类名</listener-class>
</listener>
3.2 注解配置@WebListener
filter和Listeber就总结到这里,如有任何问题,可评论,将立即改正。
版权声明
本文为[ArchieSean]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_41964942/article/details/105577442
边栏推荐
- The El table horizontal scroll bar is fixed at the bottom of the visual window
- 精度、速度完美平衡,最新图像分割SOTA模型重磅发布!!!
- mui picker和下拉刷新冲突问题
- The accuracy and speed are perfectly balanced, and the latest image segmentation SOTA model is released!!!
- three.js文字模糊问题
- Introduction to metalama 4 Use fabric to manipulate items or namespaces
- Design of body fat detection system based on 51 single chip microcomputer (51 + OLED + hx711 + US100)
- Summary of JVM knowledge points - continuously updated
- Introducing vant components on demand
- Design and manufacture of 51 single chip microcomputer solar charging treasure with low voltage alarm (complete code data)
猜你喜欢

The El table horizontal scroll bar is fixed at the bottom of the visual window

Servlet监听器&过滤器介绍

three.js文字模糊问题

AUTOSAR from introduction to mastery 100 lectures (81) - FIM of AUTOSAR Foundation

CVPR 2022 & ntire 2022 | the first transformer for hyperspectral image reconstruction

CMSIS cm3 source code annotation

Free and open source charging pile Internet of things cloud platform

Important knowledge of network layer (interview, reexamination, term end)

nodeJs + websocket 循环小案例

JMeter operation redis
随机推荐
The first lesson is canvas, showing a small case
Servlet监听器&过滤器介绍
Free and open source agricultural Internet of things cloud platform (version: 3.0.1)
Install nngraph
Nodejs + Mysql realize simple registration function (small demo)
5 free audio material websites, recommended collection
AUTOSAR from introduction to mastery 100 lectures (86) - 2F of UDS service foundation
nodeJs + websocket 循环小案例
pyqt5 将opencv图片存入内置SQLlite数据库,并查询
Date time type in database
内核错误: No rule to make target ‘debian/canonical-certs.pem‘, needed by ‘certs/x509_certificate_list‘
Mui + hbuilder + h5api simulate pop-up payment style
Nodejs + websocket cycle small case
Melt reshape decast long data short data length conversion data cleaning row column conversion
拥抱机器视觉新蓝海,冀为好望开启数字经济发展新“冀”遇
CMSIS cm3 source code annotation
4.22 study record (you only did water problems in one day, didn't you)
mui + hbuilder + h5api模拟弹出支付样式
100 GIS practical application cases (53) - making three-dimensional image map as the base map of urban spatial pattern analysis
[Technical Specification]: how to write technical documents?