10 过滤器和监听器
生活随笔
收集整理的這篇文章主要介紹了
10 过滤器和监听器
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.過(guò)濾器
1.作用:對(duì)所有請(qǐng)求或響應(yīng)做攔截操作2.開(kāi)發(fā)Filter1.自定義一個(gè)XxxFilter實(shí)現(xiàn)javax.servlet.Filter接口2.web.xml中配置(注解@webFilter(""))<filter><filter-name>XxxFilter</filter-name><filter-class></filter-class></filter><filter-mapping><filter-name>XxxFilter</filter-name>//攔截的資源<url-pattern></url-pattern></filter-mapping>3.Filter的生命周期生命周期方法:init(FilterConfig config) doFiler(ServletRequest request,ServletResponse response,FilterChain chain) destory()執(zhí)行的流程:啟動(dòng)服務(wù)器時(shí),就會(huì)執(zhí)行構(gòu)造器和init方法 每一次請(qǐng)求都會(huì)執(zhí)行doFilter方法 chain,doFilter(req,resp):放行 4.配置初始化參數(shù) 5.請(qǐng)求編碼過(guò)濾器 6.攔截所有請(qǐng)求,用初始化配置來(lái)配置不用攔截的uri,放行,其他uri如果用戶名存在才放行他uri2.Listener(監(jiān)聽(tīng)器)
1.配置:<Listener><Listener-class></Listener-class></Listener>2.使用:要使用哪個(gè)監(jiān)聽(tīng)器,就去實(shí)現(xiàn)對(duì)應(yīng)的接口3.常用的事件監(jiān)聽(tīng)1.對(duì)作用域?qū)ο蟮膭?chuàng)建銷毀監(jiān)聽(tīng)ServletRequestListenerHttpSessionListenerServletContextListener2.對(duì)WEB作用域?qū)ο蟮膶傩?添加、替換、刪除)方法進(jìn)行監(jiān)聽(tīng):ServletRequestAttributeListenerHttpSessionAttributeListenerServletContextAttributeListener轉(zhuǎn)載于:https://blog.51cto.com/10913595/2071346
總結(jié)
以上是生活随笔為你收集整理的10 过滤器和监听器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: vuejs构建的单页面应用history
- 下一篇: top进阶命令htop运用