JavaWeb监听器、过滤器、

JavaWeb监听器:域监听器

Ø  ServletContext

¨      生命周期监听:ServletContextListener

²  创建SErvletcontext触发:voidcontextInitialized(ServletContextEvent sce)

²  销毁Servletcontext触发:voidcontextDestroyed(ServletContextEvent sce)

¨      属性监听:ServletContextAttributeListener

²  添加属性时:voidattributeAdded(ServletContextAttributeEvent event);

²  替换属性时:voidattributeReplaced(ServletContextAttributeEvent event);

²  移除属性时:voidattributeRemoved(ServletContextAttributeEvent event);

Ø  HttpSession

¨      生命周期监听:HttpSessionListener

²  创建session时:voidsessionCreated(HttpSessionEvent se)

²  销毁session时:voidsessionDestroyed(HttpSessionEvent se)

¨      属性监听:HttpSessioniAttributeListener

²  添加属性时:voidattributeAdded(HttpSessionBindingEvent event);

²  替换属性时:voidattributeReplaced(HttpSessionBindingEvent event)

²  移除属性时:voidattributeRemoved(HttpSessionBindingEvent event)

Ø  ServletRequest

¨      生命周期监听:ServletRequestListener

²  创建request时:voidrequestInitialized(ServletRequestEvent sre)

²  销毁request时:voidrequestDestroyed(ServletRequestEvent sre)

¨      属性监听:ServletRequestAttributeListener。

²  添加属性时:voidattributeAdded(ServletRequestAttributeEvent srae)

²  替换属性时:voidattributeReplaced(ServletRequestAttributeEvent srae)

²  移除属性时:void attributeRemoved(ServletRequestAttributeEvent srae)

过滤器

1.        作用:它会在一组资源的前面执行,具有拦截请求的能力。与Servlet类似是单例的。

2.        实现方法:

1)        写一个类实现Filter接口:需实现接口中的3个生命周期方法

voidinit(FilterConfig):创建之后马上执行,用来初始化Filter实例。只调用一次。

a)        参数:FilterConfig-->与ServletConfig相似

              * 获取初始化参数:getInitParameter()

              * 获取过滤器名称:getFilterName()

           * 获取appliction:getServletContext()

2)        void destory():销毁之前执行,在服务器关闭时销毁。只调用一次。

3)        void doFilter(ServletRequest,ServletResponse,FilterChain):每次过滤时都会执行

Ø  FilterChain:重要方法doFilter(ServletRequest, ServletResponse),作用是放行,即执行目标资源,或是执行下一个过滤器。如果没有下一个过滤器那么执行的是目标资源,如果有,那么就执行下一个过滤器

4)        过滤器的四种拦截方式

a)        <dispatcher>REQUEST</dispatcher>:默认的,对请求进行拦截

b)        <dispatcher>FORWARD</dispatcher>:对转发进行拦截

c)        <dispatcher>INCLUDE</dispatcher>:对包含进行拦截

d)        <dispatcher>ERROR</dispatcher>:对错误进行拦截

3.        在web.xml中进行配置

<filter>

 <filter-name>xxx</filter-name>

 <filter-class>cn.itcast.web.filter.AFitler</fitler-class>

</filter>

<fitler-mapping>

  <filter-name>xxx</filter-name>

 <url-pattern>/*</url-pattern>

</filter-mapping>

1)        若有多个拦截器,配置顺序就是拦截器的执行顺序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值