小鸡炸

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Filter, Listener对象

一、Filter

1、在请求所有Servlet之前触发、类似于保安角色
2、一般用来与做全局配置、给所有Servlet初始化配置(编码、跨域、权限)

二、Filter生命周期

1、init() 初始化方法、只执行一次
2、doFilter() 过滤方法、匹配一次执行一次、
3、destroy() 销毁方法、只执行一次
4、doFilter()的chain.doFilter(res, resp);方法属于放行、如果没有这个方法则不再往下执行

三、Filter的匹配规则、Servlet也同样适用

路由规则 作用
1、/user 精确匹配
2、/user/* 路径匹配(/user/所有路径)无论多少级目录
3、*.action 后缀名匹配
4、/ 缺省匹配
1、匹配优先级 1>2>3>4
2、路径匹配和扩展名匹配无法同时设置
3、/*用在Filter、用来匹配所有资源;
4、/ *用在Servlet用来匹配所有资源内容(注意优先级)、但不包含静态资源,很恶劣;/用在Servlet、覆盖默认DefalutServlet、静态资源无法访问

四、Listener

用来监听Application、Session、Request三个对象的生命周期和属性变化
监听接口和方法 作用
HttpSessionListener(sessionCreated、sessionDestroyed) 监听Session对象创建和销毁
HttpSessionAttributeListener(attributeAdded、attributeRemoved、attributeReplaced) 监听Session属性的添加、删除、替换
ServletContextListener(ServletContextEvent、contextDestroyed) 监听Application对象的创建和销毁
ServletContextAttributeListener(attributeAdded、attributeRemoved、attributeReplaced) 监听Application属性的添加、删除、替换
ServletRequestListener(requestInitialized、requestDestroyed) 监听Request对象创建和销毁
ServletRequestAttributeListener(attributeAdded、attributeRemoved、attributeReplaced) 监听Request属性的添加、删除、替换

posted on   小鸡炸  阅读(33)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示